Skip to content

Instantly share code, notes, and snippets.

@gmariette
Last active November 10, 2021 00:06
Show Gist options
  • Select an option

  • Save gmariette/8d8e0b414ef4c74eb38c92844a6fa426 to your computer and use it in GitHub Desktop.

Select an option

Save gmariette/8d8e0b414ef4c74eb38c92844a6fa426 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: 2010-09-09
Description: Medium custom lambda stack
Resources:
CustomBackedLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: CustomBackedLambda
Runtime: python3.9
Role: my_iam_role
Handler: index.lambda_handler
Timeout: 90
Code:
ZipFile: |
import cfnresponse
import logging
import random
# Init of the logging module
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def lambda_handler(event, context):
if event.get('RequestType') == 'Create':
number = random.randint(0,1000)
message = f"This code is finally working after {number} times!" if number > 500 else f"This crappy code is still not working after {number} times!"
responseData = {}
responseData['message'] = message
logging.info('Sending %s to cloudformation', responseData['message'])
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
elif event.get('RequestType') == 'Delete':
responseData = {}
responseData['message'] = "Goodbye from lambda"
logging.info('Sending %s to cloudformation', responseData['message'])
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
else:
logging.error('Unknown operation: %s', event.get('RequestType'))
Environment:
Variables:
myvar: var01
Description: Custom lambda cloudformation
InvokeCustomLambda:
DependsOn: CustomBackedLambda
Type: Custom::InvokeCustomLambda
Properties:
ServiceToken: !GetAtt CustomBackedLambda.Arn
Outputs:
CustomLambdaOutput:
Description: Message from custom lambda
Value: !GetAtt InvokeCustomLambda.message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment