AWS Cloudformation支持直接连接

时间:2019-08-07 15:38:35

标签: amazon-web-services amazon-cloudformation aws-direct-connect

您可以通过CloudFormation设置直接连接连接吗?我似乎找不到任何明确表明CloudFormation不支持此功能的文档。我查看了CloudFormation支持的资源,但没有“直接连接”类型,但是我想知道是否可以通过使用其他类型的组合(例如VPNGateway,VPNGatewayRoutePropagation等)来创建。

1 个答案:

答案 0 :(得分:1)

您可以创建一个自定义资源来实现此目的。

MyCustomResource: 
  Type: "Custom::TestLambdaCrossStackRef"
  Properties: 
    ServiceToken:
      !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${LambdaFunctionName}
    StackName: 
      Ref: "NetworkStackName"

自定义资源文档:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html

使用最佳实践对lambda函数进行示例,以返回成功或失败。

import json
import time
from botocore.vendored import requests
def lambda_handler(event, context):
    print 'REQUEST BODY:n' + str(event)
    count = 1
    #count = int(event['ResourceProperties']['count'])    #Uncomment this line if you are configuring the number of retries through the CFN template
    attempts = 0
    if count <= 3:
        count = 3
    while attempts < count:
        try:
            if event['RequestType'] == 'Delete':
                print "delete"
                #The rest of your delete logic goes here
            elif event['RequestType'] == 'Create':
                print "create"
                #The rest of your create logic goes here
            elif event['RequestType'] == 'Update':
                print "update"
                #The rest of your update logic goes here
            responseStatus = 'SUCCESS'
            responseData = {'Success': 'Everything worked.'}
            break
        except:
            responseStatus = 'FAILURE'
            responseData = {'Failure': 'Something bad happened.'}
            attempts += 1
            time.sleep(3)
    sendResponse(event, context, responseStatus, responseData)

def sendResponse(event, context, responseStatus, responseData, reason=None, physical_resource_id=None):
    responseBody = {'Status': responseStatus,
                    'Reason': 'See the details in CloudWatch Log Stream: ' + context.log_stream_name,
                    'PhysicalResourceId': physical_resource_id or context.log_stream_name,
                    'StackId': event['StackId'],
                    'RequestId': event['RequestId'],
                    'LogicalResourceId': event['LogicalResourceId'],
                    'Data': responseData}
    print 'RESPONSE BODY:n' + json.dumps(responseBody)
    responseUrl = event['ResponseURL']
    json_responseBody = json.dumps(responseBody)
    headers = {
        'content-type' : '',
        'content-length' : str(len(json_responseBody))
    }
    try:
        response = requests.put(responseUrl,
                                data=json_responseBody,
                                headers=headers)
        print "Status code: " + response.reason
    except Exception as e:
        print "send(..) failed executing requests.put(..): " + str(e)