aws-cloudformation-templates icon indicating copy to clipboard operation
aws-cloudformation-templates copied to clipboard

AWS CloudFormation stack giving CREATE_FAILED state after custom resource (lambda) invocation

Open nikitakakra opened this issue 5 years ago • 1 comments

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 custom resource lambda to create application.properties in SSM. 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"
         }
      }
   }
}

The python version which I am using is 2.7. My python script to create SSM parameter (whose path is: /myapp/dev/test/application.properties) is:

import boto3
import os
import logging
import json
from botocore.vendored import requests

logger = logging.getLogger()
logger.setLevel(logging.INFO)

region = os.environ['AWS_REGION']

client = boto3.client('ssm')
print(region)

def test_ssm_create():
    response = client.put_parameter(Name='/myapp/'
                                    + os.environ['environment']
                                    + '/test/application.properties',
                                    Description='string',
                                    Value='APPLICATION_NAME=myapp,\nTEST_URL=,\nS3_BUCKET=,\nAWS_REGION='+region+',\nPAGE_SIZE=20,\nMAX_RECORDS=20',
                                    Type='SecureString', Overwrite=True)
    return response

def lambda_handler(event, context):
  
    logger.info(event)
    PutParameterResult = test_ssm_create()
    status = {'Status': 'SUCCESS'}
    r = requests.put(event['ResponseURL'], data=json.dumps(status))
    
    print("COMPLETE")

When I try to create the stack, it gives an error CREATE_FAILED, Invalid PhysicalResourceId. I need to basically return a status of success to the cloudformation template so that it completes stack creation successfully. Am I supposed to return this PhysicalResourceId from my script to make it work? Any help would be greatly appreciated.

Note: I read at many places that cfnresponse comes by default with Python 2.7. cfnresponse can be used to return a status of 'SUCCESS' back to the cloudformation stack. So I used import cfnresponse as suggested in this link but the import doesn't work. It gives an error saying Unable to import module 'lambda_function': No module named 'cfnresponse' and I don't know how to install cfnresponse library externally onto the lambda.

nikitakakra avatar Oct 17 '19 06:10 nikitakakra

you may want to move away from python 2.7 as AWS no longer supports and NEW lambdas using the python 2.7 runtime

tw33dledee avatar Feb 19 '20 04:02 tw33dledee

Due to inactivity this issue will be closed in 7 days

github-actions[bot] avatar May 03 '24 19:05 github-actions[bot]