- Más nuevo
- Más votos
- Más comentarios
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:
- Upload the Code to S3
- Obtain the Version Number in the S3 console in the
Versions
tab - Add the Version Number to the third parameter in the
lambda.S3Code()
function - 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.
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'])
}
===
Contenido relevante
- ¿Cómo puedo hacer una llamada al SDK de AWS desde un proyecto de CDK mediante la interfaz AWSdkCall?OFICIAL DE AWSActualizada hace 10 meses
- OFICIAL DE AWSActualizada hace 2 años