I invoke a lambda function from a custom resource in cloudformation template. This custom resource has a property : the name of the cloudformation stack. I need this property value in the lambda function so that I can get other variables like parameters and outputs of the stack. Here is the custom resource:
"DeployApp": {
"Type" : "Custom::deployapplication",
"Properties" : {
"ServiceToken": { "Fn::GetAtt" : ["lambda" , "Arn"] },
"StackName": { "Ref": "AWS::StackName" }
}
}
For the moment, I get the stack name in the lambda function this way:
import boto3
def lambda_handler(event, context):
cf_client = boto3.client('cloudformation')
# Get the name of the stack
stack = context.invoked_function_arn.split(':')[6]
stack = stack.split('-')[:4]
stack_name = stack[0]+'-'+stack[1]+'-'+stack[2]+'-'+stack[3]
print('CloudFormation stack name : '+stack_name)
Here I always set the name of the stack to something like this:
stack-cfn-lambda-app : 4 words split by '-'
But I want to make it more general so that all names formats are accepted.
How to do that and get the exact name of the stack without knowing already how the format is going to be?
In other words, how in python I can get from this string (function lambda ARN):
arn:aws:lambda:eu-west-1:53436693423891:function:stackName-ELATEDLEXZPS
the string between :function: and ELATEDLEXZPS ?