如何使用 cloudformation 对 AWS 实例中的应用程序调用一系列 API 调用



有没有办法创建一个云形成模板,该模板调用对EC2实例的REST API调用?

用例是修改应用程序的配置,而不必使用更新堆栈和用户数据,因为用户数据更新具有中断性。

我确实搜索了所有文档,发现这可以通过调用 AWS lambda 来完成。但是,无法获得 CFM 模板和调用属性的正确组合。

添加一个简单的lambda,它可以独立工作:

from __future__ import print_function
import requests
def handler(event, context):
r1=requests.get('https://google.com')
message = r1.text
return { 
'message' : message
}  

这被命名为 ltest.py,并打包到 ltest.zip 带有请求模块等.ltest.zip然后在 CFM 模板中调用:

{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Test",
"Parameters": {
"ModuleName" : {
"Description" : "The name of the Python file",
"Type" : "String",
"Default" : "ltest"
},
"S3Bucket" : {
"Description" : "The name of the bucket that contains your packaged source",
"Type" : "String",
"Default" : "abhinav-temp"
},
"S3Key" : {
"Description" : "The name of the ZIP package",
"Type" : "String",
"Default" : "ltest.zip"
}
},
"Resources" : {
"AMIInfo": {
"Type": "Custom::AMIInfo",
"Properties": {
"ServiceToken": { "Fn::GetAtt" : ["AMIInfoFunction", "Arn"] }
}
},
"AMIInfoFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": { "Ref": "S3Bucket" },
"S3Key": { "Ref": "S3Key" }
},
"Handler": { "Fn::Join" : [ "", [{ "Ref": "ModuleName" },".handler"] ]},
"Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },        
"Runtime": "python2.7",
"Timeout": "30"
}
},
"LambdaExecutionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": ["lambda.amazonaws.com"]},
"Action": ["sts:AssumeRole"]
}]
},
"Path": "/",
"Policies": [{
"PolicyName": "root",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["logs:CreateLogGroup","logs:CreateLogStream","logs:PutLogEvents"],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": ["ec2:DescribeImages"],
"Resource": "*"
}]
}
}]
}
}    
},

"输出" : { "阿米德" : { "描述": "结果", "Value" : { "Fn::GetAtt": [ "AMIInfo", "message" ] } }
} }

上述结果(使用 Fn::GetAtt 调用的变体)是 Lambda 被实例化,但 AMIInfo 调用卡在"CREATE_FUNCTION"中。

堆栈也不会被正确删除。

我会用 Lambda 攻击它,但似乎你已经想到了这一点,并且可能会忽略它。

有点黑客,但是您可以通过源是 REST URL 的元数据将文件添加到实例中吗?

例如

"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"configSets": {
"CallREST": [ "CallREST" ]
},
"CallREST": { "files": 
{ "c://cfn//junk//rest1output.txt": { "source": "https://myinstance.com/RESTAPI/Rest1/Action1" } } },
}
}

要修复您的 lambda,您需要发出成功信号。 当 CloudFormation 创建(并运行)Lambda 时,它期望 Lambda 表示成功。 这就是您遇到卡住"CREATE_IN_PROGRESS"的原因

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html 的底部是一个名为"发送"的函数,可帮助您发出成功的信号。

这是我尝试将其作为 PSUEDOCODE 集成到您的函数中而不对其进行测试,但您应该明白这个想法。

from __future__ import print_function
import requests
def handler(event, context):
r1=requests.get('https://google.com')
message = r1.text
# signal complete to CFN
# send(event, context, responseStatus, responseData, physicalResourceId)
send(..., ..., SUCCESS, ...)
return { 
'message' : message
}  

由事件触发的 Lambda。生命周期挂钩可能会有所帮助。 你可以破解CoudFormation,但请注意:它不是为此而设计的。

相关内容

最新更新