CDK Lambda Deployment

0

Hello, So I'm using CDK to build my infrastructure. I have created a Lambda Function in my CDK and another folder that holds the Lambda code. I used the following commands to deploy it to my AWS account in the command line:

npm run build && cdk synth cdk bootstrap cdk deploy Everything worked fine the first time. However when I make any changes to my Lambda code and re-use these commands again my lambda code is not modified on the console. Seems like its not deploying the code because there is no changes in the CDK. How do I deploy my modified lambda code then? Can't find any documentation on that.

2回答
2
承認された回答

Since CDK uses CloudFormation to deploy its resources, any updates will need to trigger a CloudFormation update so that the code will be updated. As a test, I used the following CDK Lambda Code. Please see below:

const bucket = s3.Bucket.fromBucketName(this, 'bucket', '<Bucket_Name>');

      const lambdaFunc = new lambda.Function(this, 'lambdaFunc', {
          runtime: lambda.Runtime.PYTHON_3_9,
          code: new lambda.S3Code(bucket, "hello.py.zip", '<S3_Object_Version_Number>'),
          handler: 'lambda.lambda_handler',
    });

Since I am leveraging S3 as the source of my code, any changes to the code will require the following steps:

  1. Upload the Code to S3
  2. Obtain the Version Number in the S3 console in the Versions tab
  3. Add the Version Number to the third parameter in the lambda.S3Code() function
  4. run the cdk deploy command

After the Stack updated successfully, I was able to confirm that my Lambda code changed in the Lambda Console. Please see the test results below:

=== Original:

def handler(event, context):
    print('request: {}'.format(json.dumps(event)))
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/plain'
        },
        'body': 'Hello, CDK! You have a hit {}\n'.format(event['path'])
    }

Updated:

def handler(event, context):
    print('request: {}'.format(json.dumps(event)))
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/plain'
        },
        'body': 'Hola, CDK! Tienes un éxito {}\n'.format(event['path'])
    }

===

Since there are various ways to source the Code in CDK for Lambda Constructs, a code snippet and CDK version will help me in provide the appropriate update steps for your use case.

profile picture
エキスパート
回答済み 1年前
2

I noticed that you mentioned that the Lambda code is in another file. My apologies for missing this detail earlier.

As a test, I deployed a CDK Stack that leveraged the Lambda construct. Please see the CDK code below:

 const lambdaFunc = new lambda.Function(this, 'lambdaFunc', {
          runtime: lambda.Runtime.PYTHON_3_9,
          code: lambda.Code.fromAsset('lambda'),
          handler: 'hello.handler',
    });
  }

The Lambda code will need to be in the in the CDK App directory but does not have to be in the bin nor the lib directory. In my example, the handler function is defined in the hello.py file that is in the lambda folder in my app.

Once my stack was deployed, I made the changes to the hello.py file and, then, ran the cdk deploy command. CDK was able to detect that the file changed and made the update to the Lambda function via CloudFormation. Please see the test results below:

=== Original:

def handler(event, context):
    print('request: {}'.format(json.dumps(event)))
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/plain'
        },
        'body': 'Hola, CDK! Tienes un éxito {}\n'.format(event['path'])
    }

Updated:

def handler(event, context):
    print('request: {}'.format(json.dumps(event)))
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/plain'
        },
        'body': 'Hello, CDK! You have a hit {}\n'.format(event['path'])
    }

===

profile picture
エキスパート
回答済み 1年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ