我们有一个远程API(非AWS),我们可以从中读取参数值。 我们可以在cloudformation中读取这些值并将它们用作值吗?
或者是唯一可能的方法来获取值并使用aws cli
提供它们并将值作为参数值传递给部署命令。
答案 0 :(得分:3)
您可以使用cloudformation custom resource调用lambda函数解析API输出并将其发送回cloudformation并通过!GetAtt
Cloudformation:
Resources:
API:
Type: Custom::API
Version: '1.0'
Properties:
ServiceToken: arn:aws:lambda:us-east-1:acc:function:CALL_API
Outputs:
Status:
Value:
Fn::GetAtt:
- API
- Data
Lambda脚本:
import json
import cfnresponse
import boto3
import urllib.request
from botocore.exceptions import ClientError
def handler(event, context):
responseData = {}
try:
with urllib.request.urlopen("http://maps.googleapis.com/maps/api/geocode/json?address=google") as url:
data = json.loads(url.read().decode())
print(data)
responseData['Data'] = data
status=cfnresponse.SUCCESS
except ClientError as e:
responseData['Data'] = "FAILED"
status=cfnresponse.FAILED
print("Unexpected error: %s" % e)
cfnresponse.send(event, context, status, responseData, "CustomResourcePhysicalID")