1

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

4

1 回答 1

2

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

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)
于 2020-01-31T08:47:14.707 回答