<p>似乎您希望在部署CloudFormation堆栈时触发AWS Lambda函数。你知道吗</p>
<p>你可以用一个<a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources-lambda.html" rel="nofollow noreferrer">AWS Lambda-backed Custom Resource</a>来实现这一点。你知道吗</p>
<p>模板应包括:</p>
<ul>
<li>Lambda函数</li>
<li>触发Lambda函数的<code>Custom::</code>项</li>
</ul>
<p>Lambda函数完成后需要发回信号。提供了一个<a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html" rel="nofollow noreferrer">cfn-response Module</a>来帮助实现这一点。它可用于节点.js还有Python。你知道吗</p>
<p>以下是部署和运行自定义资源的基本CloudFormation模板:</p>
<pre class="lang-py prettyprint-override"><code>AWSTemplateFormatVersion: 2010-09-09
Resources:
LambdaBasicExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: MyLambdaRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
LambdaFunctionTest:
Type: AWS::Lambda::Function
DependsOn: LambdaBasicExecutionRole
Properties:
FunctionName: LambdaFunctionTest
Description: Lambda Function to test that Custom Resource works
Handler: index.handler
Runtime: python3.6
Role: !GetAtt LambdaBasicExecutionRole.Arn
Code:
ZipFile: |
import boto3
import cfnresponse
def handler(event, context):
print('This is in the handler')
responseData = {}
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
return
CustomFunctionTest:
Type: Custom::CustomFunctionTest
Properties:
ServiceToken: !GetAtt LambdaFunctionTest.Arn
</code></pre>