Skip to content

AWS Lambda not receiving parameters passed from custom resource  #456

@dhirajkhodade

Description

@dhirajkhodade

❓ Guidance Question

The Question

I am trying to create lambda based custom resource using AWS CDK but the parameters that I am passing to lambda using ResourceProperties are not being received in lambda event object.

I am not sure what am I doing wrong.

I am getting below error while deploying stack

Received response status [FAILED] from custom resource. Message returned: Error: 'EbEnvironmentName'

CDK Code

import * as cdk from '@aws-cdk/core';
import { CustomResource } from '@aws-cdk/core';
import { Provider } from '@aws-cdk/custom-resources';
import lambda = require('@aws-cdk/aws-lambda');

export class CreateEbInfraDevDKStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

   
    const lc_patcher_fn = new lambda.Function(this, `eb_launch_config_patcher`, {
      runtime: lambda.Runtime.PYTHON_3_7,
      code: lambda.Code.fromAsset('./custom_resource_handlers/'),
      handler: 'eb_launch_config_patcher.lambda_handler',
    });

    const lc_patcher_provider = new Provider(this, 'lc_patcher_provider', {
      onEventHandler: lc_patcher_fn,
    });
    
    const customResource = new  CustomResource(this, `CustomResource`, {
      serviceToken: lc_patcher_provider.serviceToken,
      properties: {
        "EbEnvironmentName": "test-parameter",
        "Region": "us-east-1"
      }
    });
  }
}

lambda function code

import os
import sys
import json
import boto3
import base64


def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    props = event['ResourceProperties']
    aws_region = props['Region']
    eb_environment_name = props['EbEnvironmentName']

lambda log

{
"RequestType": "Create",
"ServiceToken": "arn:aws:lambda:us-east-1:XXXX:function:CreateEbInfraDevStack-lcpatcherproviderframeworkon-Nk9Go2S4g4sh",
"ResponseURL": "https://cloudformation-custom-resource-response-XXXXXXX",
"StackId": "arn:aws:cloudformation:us-east-1:XXXXX:stack/CreateEbInfraDevStack/12f54120-d31c-11eb-a9b3-0e443e7db6f3",
"RequestId": "5f24fc02-2cd4-49e4-a61b-3258b7de3fb4",
"LogicalResourceId": "EBTestCustomResource",
"ResourceType": "AWS::CloudFormation::CustomResource",
"ResourceProperties": {
	"ServiceToken": "arn:aws:lambda:us-east-1:XXXXXX:function:CreateEbInfraDevStack-lcpatcherproviderframeworkon-Nk9Go2S4g4sh",
	"Region": "us-east-1"
	}
}


[ERROR] KeyError: 'EbEnvironmentName'
Traceback (most recent call last):
  File "/var/task/eb_launch_config_patcher.py", line 171, in lambda_handler
    eb_environment_name = props['EbEnvironmentName']

Environment

  • CDK CLI Version: 1.105.0
  • Example: custom-resource
  • Example Version:
  • OS: Windows 10
  • Language: TypeScript

Other information

I am following this CDK example - https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/custom-resource

Metadata

Metadata

Assignees

No one assigned

    Labels

    guidanceQuestion that needs advice or information.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions