gpt4 book ai didi

amazon-web-services - 如何使用 cloudformation 对 AWS 实例中的应用程序进行一系列 API 调用

转载 作者:行者123 更新时间:2023-12-03 07:32:39 25 4
gpt4 key购买 nike

有没有办法创建一个 cloudformation 模板来调用 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,与requests模块等一起打包成ltest.zip,然后在CFM模板中调用ltest.zip:

{
"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": "*"
}]
}
}]
}
}
},

“输出”:{ “AMIID”:{ “描述”:“结果”, “值”:{“Fn::GetAtt”:[“AMIInfo”,“消息”]} } }
}

上述结果(带有 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 的底部是一个名为“send”的函数,可帮助您发出成功信号。

这是我尝试将其集成到您的函数 AS 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
}

关于amazon-web-services - 如何使用 cloudformation 对 AWS 实例中的应用程序进行一系列 API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com