Reputation: 511
Can CloudFormation provision a Direct Connect Connection? There aren't Direct Connect CloudFormation resource type(s), but I'm wondering if it can be created by using a combination of other types like a VPNGateway, VPNGatewayRoutePropagation, etc.
Upvotes: 1
Views: 2716
Reputation: 390
You can create a custom resource:
MyCustomResource:
Type: "Custom::TestLambdaCrossStackRef"
Properties:
ServiceToken:
!Sub arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:${LambdaFunctionName}
StackName:
Ref: "NetworkStackName"
Custom Resource Docs: 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
Sample lambda function with best practices to return success or failure:
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 if configuring the number of retries through the CFN template
attempts = 0
if count <= 3:
count = 3
while attempts < count:
try: # rest of your logic goes here
if event['RequestType'] == 'Delete':
pass
elif event['RequestType'] == 'Create':
pass
elif event['RequestType'] == 'Update':
pass
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': '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))
Upvotes: 2