I have a lambda that has a python script to create an application.properties file in parameter store. I have a cloudformation template that invokes this lambda to create application.properties. My cloudformation template looks like:
{
"Description": "Create SSM Parameter",
"Resources": {
"primerinvoke": {
"Type": "AWS::CloudFormation::CustomResource",
"Properties": {
"Handler": "lambda_function.lambda_handler",
"ServiceToken": "arn:aws:lambda:us-east-1:1234:function:test_lambda",
"FunctionName": "test_lambda"
}
}
}
}
My python script to create SSM parameter (whose path is: /myapp/dev/test/application.properties) is:
import boto3
import os
region = os.environ['AWS_REGION']
client = boto3.client('ssm')
def ssm_create():
response = client.put_parameter(Name='/myapp/'
+ os.environ['environment']
+ '/test/application.properties',
Description='string',
Value='APPLICATION_NAME=myapp',
Type='SecureString', Overwrite=True)
return response
def lambda_handler(event, context):
PutParameterResult = ssm_create()
I used import cfnresponse as suggested in AWS lambda: No module named 'cfnresponse', however, the import doesn't work and I don't know how to install cfnresponse library externally on the lambda.
The code runs successfully which means that cloudformation template was able to invoke the lambda & it's script and create the SSM parameter but it hangs after ssm_create() function in my script completes. I don't know how to return a status of "SUCCESS" from my script to the cloudformation stack to prevent it from hanging in CREATE_IN_PROGRESS state.
Any help will be greatly appreciated! Thanks.
EDIT1: I updated my code to:
responseStatus = 'SUCCESS'
responseBody={}
def sendResponse(event, context, responseStatus):
responseBody = {'Status': responseStatus,
'Reason': 'See the details in CloudWatch Log Stream: ' + context.log_stream_name,
'PhysicalResourceId': context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
}
print 'RESPONSE BODY:n' + json.dumps(responseBody)
def lambda_handler(event, context):
logger.info(event)
test_ssm_create()
try:
req = requests.put(event['ResponseURL'], data=json.dumps(sendResponse(event, context, responseStatus)))
if req.status_code != 200:
print req.text
raise Exception('Recieved non 200 response while sending response to CFN.')
except requests.exceptions.RequestException as e:
print e
raise
return
print("COMPLETE")
The req.status_code is giving 200 but the cloudformation stack is again getting stuck at CREATE_IN_PROGRESS. Still not sure how to make this work.