Search code examples
pythonaws-lambdaparametersaws-cloudformationaws-cloudformation-custom-resource

CloudFormation: Reference and use Parameters in inline python code for custom resource?


//This is inside one cloudformation template

//These are the parameters, from here i want to use the OpsBucketname

AWSTemplateFormatVersion: 2010-09-09
Transform:
  - AWS::LanguageExtensions
  - AWS::Serverless-2016-10-31
Parameters:
  DeploymentHash:
    Type: String
    Description: sample description
  Stage:
    Type: String
    Description: sample description
  OpsBucketName:
    Type: String

//These are the resources where I want to use the parameter, on the lambda function, code: zipfile: - on the lambda_handler

Resources:
  CognitoUiCustomizationLambdaRole:
  Type: "AWS::IAM::Role"
   // ...
  CognitoSetUiCustomizationLambda:
    Type: "AWS::Lambda::Function"
    Properties:
      Code:
        ZipFile: |
          import boto3
          import logging

          logger = logging.getLogger()
          client = boto3.client('cognito-idp')
          s3 = boto3.client('s3')

          def lambda_handler(event, context):
              logo = s3.get_object(Bucket="${OpsBucketName}",            Key="${BlockPrefix}/assets/frontend/assets/images/logo.png") 

              // other codes here not related to questions ...
      Description: Lambda for custom resource to customize the cognito login page
      FunctionName: !Sub "CognitoLoginCustomization-${NamingSuffix}"
      Handler: index.lambda_handler
      Runtime: python3.9
      Timeout: 10
      Role: !GetAtt CognitoUiCustomizationLambdaRole.Arn

My main purpose here is to get the bucket name from the parameters and then use it to get the logo from that bucket and use that logo on the cognito_idp.setUiCustomization() function to set a custom cognito login page.

Inside my function lambda_handler im using the OpsBucketName using the syntax ${OpsBucketName} but in my cloudwatch logs,the error is it is empty but Im sure it has a value as the other resources not using a custom resource is successfully created and has successfully used that bucket name.

Is my syntax of using that parameter here correct? I cant find a sample on the internet the same with my problem. Thank you very much.

I tried searching on the documentation for this scenario but i did not see one. Im expecting to see a sample where the parameters are used on the lambda function, Code: ZipFile using python.


Solution

  • I'm not sure how you can achieve it directly from the Parameters, but then you can make use of the Environment variables inside your inline lambda function by referring to the Parameters you've defined in the Cloudformation template.

    First we need to create environment variables that point to your Parameters:

    LambdaFunction:
        Type: AWS::Lambda::Function
        Properties:
          Environment:
            Variables:
              DeploymentHash: !Ref DeploymentHash
              Stage: !Ref Stage
              OpsBucketName: !Ref OpsBucketName
    

    and then extract these environment variables within your inline code

    # extracting the env variables first
    deploymentHash = os.environ.get('DeploymentHash')
    stage = os.environ.get('Stage')
    opsbucketname = os.environ.get('OpsBucketName')
    
    def lambda_handler(event, context):
        # your code