从cloudformation中获取远程API的参数值

时间:2018-04-17 06:50:07

标签: amazon-web-services amazon-cloudformation

我们有一个远程API(非AWS),我们可以从中读取参数值。 我们可以在cloudformation中读取这些值并将它们用作值吗?

或者是唯一可能的方法来获取值并使用aws cli提供它们并将值作为参数值传递给部署命令。

1 个答案:

答案 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")