Skip to content

Commit 49116ae

Browse files
FrontierPsychiatristMoritz do Rio Schulze
andauthored
feat: allow setting a lambda version description from S3 Metadata (#179)
Co-authored-by: Moritz do Rio Schulze <mdorioschulze@stroer.de>
1 parent 279fd13 commit 49116ae

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

examples/deployment/complete/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ Note that this example may create resources which cost money. Run `terraform des
2222
Upload a new `zip` package to S3 to start the deployment pipeline:
2323

2424
```shell
25-
aws s3api put-object --bucket example-ci-{account_id}-{region} --key deployment-hooks/package/lambda.zip --body lambda.zip
25+
aws s3api put-object --bucket example-ci-{account_id}-{region} --key deployment-hooks/package/lambda.zip --metadata description="Description for the new Lambda version" --body lambda.zip
2626
```
2727

28+
The `--metadata description=` is optional. If set, it will use the value as the description for the new Lambda function
29+
version.
30+
2831
<!-- BEGIN_TF_DOCS -->
2932
## Requirements
3033

examples/deployment/s3/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ Note that this example may create resources which cost money. Run `terraform des
1616
Upload a new `zip` package to S3 to start the deployment pipeline:
1717

1818
```shell
19-
aws s3api put-object --bucket example-ci-{account_id}-{region} --key s3-deployment/package/lambda.zip --body lambda.zip
19+
aws s3api put-object --bucket example-ci-{account_id}-{region} --key s3-deployment/package/lambda.zip --metadata description="Description for the new Lambda version" --body lambda.zip
2020
```
2121

22+
The `--metadata description=` is optional. If set, it will use the value as the description for the new Lambda function
23+
version.
24+
2225
<!-- BEGIN_TF_DOCS -->
2326
## Requirements
2427

modules/deployment/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ of the CodePipeline needs an AWS S3 Notification for emitting events in your Ama
181181
filtered events to EventBridge and trigger the pipeline (see [docs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventBridge.html) for details). Make sure to enable S3 bucket
182182
notifications for your source bucket!
183183

184+
Optionally, if the S3 object being deployed has the metadata `description` set, it will be used as the description of
185+
the new published version of the Lambda, truncated at 256 characters. You are responsible for setting this metadata when
186+
you produce the object in S3. Without the metadata, the function description is copied for the version description.
187+
184188
### with custom deployment configuration
185189

186190
This module supports all predefined [default deployment configurations](https://docs.aws.amazon.com/codedeploy/latest/userguide/deployment-configurations.html)

modules/deployment/codebuild.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,21 @@ phases:
103103
versionId = os.environ.get("SOURCEVARIABLES_VERSIONID")
104104
print(f"S3 deployment: {s3_bucket}/{s3_key} (versionId={versionId})")
105105
106-
update_response = lambda_client.update_function_code(FunctionName=lambda_function_name, S3Bucket=s3_bucket, S3Key=s3_key, S3ObjectVersion=versionId, Publish=True)
107-
target_version = update_response["Version"]
106+
s3_client = boto3.client("s3", region_name=os.environ.get("REGION"))
107+
s3_attributes = s3_client.head_object(Bucket=s3_bucket, Key=s3_key, VersionId=versionId)
108+
109+
if "description" in s3_attributes["Metadata"]:
110+
description = s3_attributes["Metadata"]["description"]
111+
# The description of a lambda version can be max 256 characters, so we need to ensure that here
112+
truncated_description = description[:256]
113+
update_response = lambda_client.update_function_code(FunctionName=lambda_function_name, S3Bucket=s3_bucket, S3Key=s3_key, S3ObjectVersion=versionId, Publish=False)
114+
waiter = lambda_client.get_waiter("function_updated_v2")
115+
waiter.wait(FunctionName=lambda_function_name)
116+
publish_response = lambda_client.publish_version(FunctionName=lambda_function_name, CodeSha256=update_response["CodeSha256"], Description=truncated_description)
117+
target_version = publish_response["Version"]
118+
else:
119+
update_response = lambda_client.update_function_code(FunctionName=lambda_function_name, S3Bucket=s3_bucket, S3Key=s3_key, S3ObjectVersion=versionId, Publish=True)
120+
target_version = update_response["Version"]
108121
else:
109122
# ECR/image deployment
110123
image_uri = os.environ.get("SOURCEVARIABLES_IMAGE_URI")

0 commit comments

Comments
 (0)