-
Notifications
You must be signed in to change notification settings - Fork 432
chore(layers): add release pipeline in GitHub Actions #1278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
heitorlessa
merged 25 commits into
aws-powertools:develop
from
am29d:chore/layer-release
Jul 7, 2022
Merged
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
49ed47a
chore: add layer project
fee2d9a
reduce to 1 region for dev
cfe92fc
Merge branch 'awslabs:develop' into chore/layer-release
am29d 9f1042e
chore: shorter name for the workflow
d07e0b6
fix ignore markdown lint for now
3431528
fix: more f strings
3d49b5a
Merge branch 'awslabs:develop' into chore/layer-release
am29d e3af831
ignore mdlint
3918cbd
Merge branch 'chore/layer-release' of github.com:am29d/aws-lambda-pow…
94e3ab7
add reusable workflow for both beta and prod
bfc4980
Update layer/layer/canary/app.py
am29d 43f6df9
Update layer/layer/canary/app.py
am29d ace2df6
readme review
b1b5408
Merge branch 'chore/layer-release' of github.com:am29d/aws-lambda-pow…
b21e6f6
rephrase canary stack ssm parameter usage
5696954
add default RELEASE_TAG_VERSION assignment based on the input (releas…
c08ca46
add reference to layer docs
4045d70
wording
9df6443
move version trackign arn to canary stack
2a2a8d7
remove outdated npm caching, add release tag resolution for manual wo…
28d986a
review: fix layer name and remove dependencies from reusable workflow
b89c53e
remove debug statement, add default working dir
c4a5699
pin versions and hashes for requirements with pip-compile
5dd6b76
rename reusable workflow
b60d57b
pass artefact name to the reusable workflow to prevent potential futu…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
name: Deploy layer to all regions | ||
|
||
permissions: | ||
id-token: write | ||
contents: read | ||
|
||
on: | ||
# TODO: remove after setup is finished | ||
workflow_dispatch: { } | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
workflow_run: | ||
workflows: [ "Publish to PyPi" ] | ||
types: | ||
- completed | ||
|
||
|
||
jobs: | ||
build-layer: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '16.12' | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- name: Setup python | ||
uses: actions/setup-python@v4 | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- name: Cache dependencies | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.npm | ||
key: npm-${{ hashFiles('package-lock.json') }} | ||
restore-keys: npm- | ||
- name: Set release notes tag | ||
run: | | ||
RELEASE_TAG_VERSION=${{ github.event.release.tag_name }} | ||
echo "RELEASE_TAG_VERSION=${RELEASE_TAG_VERSION:1}" >> $GITHUB_ENV | ||
- name: install cdk and deps | ||
run: | | ||
npm install -g [email protected] | ||
cdk --version | ||
- name: install deps | ||
run: | | ||
cd layer | ||
pip install -r requirements.txt | ||
- name: CDK build | ||
run: cdk synth --context version=$RELEASE_TAG_VERSION -o cdk.out | ||
- name: zip output | ||
run: zip -r cdk.out.zip cdk.out | ||
- name: Archive CDK artifacts | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: cdk.out | ||
path: cdk.out.zip | ||
|
||
deploy-beta: | ||
runs-on: ubuntu-latest | ||
needs: | ||
- build-layer | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
region: [ | ||
"af-south-1", | ||
# "eu-central-1", | ||
# "us-east-1", | ||
# "us-east-2", | ||
# "us-west-1", | ||
# "us-west-2", | ||
# "ap-east-1", | ||
# "ap-south-1", | ||
# "ap-northeast-1", | ||
# "ap-northeast-2", | ||
# "ap-southeast-1", | ||
# "ap-southeast-2", | ||
# "ca-central-1", | ||
# "eu-west-1", | ||
# "eu-west-2", | ||
# "eu-west-3", | ||
# "eu-south-1", | ||
# "eu-north-1", | ||
# "sa-east-1", | ||
# "ap-southeast-3", | ||
# "ap-northeast-3", | ||
# "me-south-1" | ||
] | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- name: aws credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-region: ${{ matrix.region }} | ||
role-to-assume: arn:aws:iam::${{ secrets.LAYERS_BETA_ACCOUNT }}:role/${{ secrets.AWS_GITHUB_OIDC_ROLE }} | ||
- name: Print assumed role | ||
run: aws sts get-caller-identity | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '16.12' | ||
- name: Setup python | ||
uses: actions/setup-python@v4 | ||
- name: Cache dependencies | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.npm | ||
key: npm-${{ hashFiles('package-lock.json') }} | ||
restore-keys: npm- | ||
- name: install cdk and deps | ||
run: | | ||
npm install -g [email protected] | ||
cdk --version | ||
- name: install deps | ||
run: | | ||
cd layer | ||
pip install -r requirements.txt | ||
- name: Download a single artifact | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: cdk.out | ||
- name: unzip cdk.out.zip | ||
run: unzip cdk.out.zip | ||
- name: CDK Deploy Layer | ||
run: cdk deploy --app cdk.out --context region=${{ matrix.region }} 'PowertoolsLayerStack' --require-approval never --verbose | ||
- name: CDK Deploy Canary | ||
run: cdk deploy --app cdk.out --context region=${{ matrix.region}} --parameters DeployStage="BETA" 'CanaryStack' --require-approval never --verbose | ||
|
||
deploy-prod: | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
runs-on: ubuntu-latest | ||
needs: | ||
- deploy-beta | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
region: [ | ||
"af-south-1", | ||
# "eu-central-1", | ||
# "us-east-1", | ||
# "us-east-2", | ||
# "us-west-1", | ||
# "us-west-2", | ||
# "ap-east-1", | ||
# "ap-south-1", | ||
# "ap-northeast-1", | ||
# "ap-northeast-2", | ||
# "ap-southeast-1", | ||
# "ap-southeast-2", | ||
# "ca-central-1", | ||
# "eu-west-1", | ||
# "eu-west-2", | ||
# "eu-west-3", | ||
# "eu-south-1", | ||
# "eu-north-1", | ||
# "sa-east-1", | ||
# "ap-southeast-3", | ||
# "ap-northeast-3", | ||
# "me-south-1" | ||
] | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
repository: ${{ github.event.pull_request.head.repo.full_name }} | ||
- name: aws credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-region: ${{ matrix.region }} | ||
role-to-assume: arn:aws:iam::${{ secrets.LAYERS_PROD_ACCOUNT }}:role/${{ secrets.AWS_GITHUB_OIDC_ROLE }} | ||
- name: Print assumed role | ||
run: aws sts get-caller-identity | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '16.12' | ||
- name: Setup python | ||
uses: actions/setup-python@v4 | ||
- name: Cache dependencies | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/.npm | ||
key: npm-${{ hashFiles('package-lock.json') }} | ||
restore-keys: npm- | ||
- name: install cdk and deps | ||
run: | | ||
npm install -g [email protected] | ||
cdk --version | ||
- name: install deps | ||
run: | | ||
cd layer | ||
pip install -r requirements.txt | ||
- name: Download a single artifact | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: cdk.out | ||
- name: unzip cdk.out.zip | ||
run: unzip cdk.out.zip | ||
- name: CDK Deploy Layer | ||
run: cdk deploy --app cdk.out --context region=${{ matrix.region }} 'PowertoolsLayerStack' --require-approval never --verbose | ||
- name: CDK Deploy Canary | ||
run: cdk deploy --app cdk.out --context region=${{ matrix.region}} --parameters DeployStage="PROD" 'CanaryStack' --require-approval never --verbose |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.swp | ||
package-lock.json | ||
__pycache__ | ||
.pytest_cache | ||
.venv | ||
*.egg-info | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!-- markdownlint-disable MD041 MD043--> | ||
# CDK Powertools layer | ||
|
||
This is a CDK project to build and deploy AWS Lambda Powertools layer to multiple commercial regions. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Build the layer | ||
|
||
To build the layer construct you need to provide the powertools version. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
You can pass it as a context variable when running `synth` or `deploy`, | ||
|
||
```shell | ||
cdk synth --context version=1.25.1 | ||
``` | ||
|
||
## Canary stack | ||
|
||
We use a canary stack to verify that the deployment is successful and we can use the layer by adding it to a newly created Lambda function. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The canary is deployed after the layer construct. Because the layer ARN is created during the deploy we need to pass this information async via SSM parameter. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
To achieve that, we establish a naming convention for the SSM key. The layer construct knows where to write the layer ARN after the deployment and the Canary stacks know where to read this information. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Version tracking | ||
|
||
AWS Lambda versions the layers by incrementing a number at the end of the arn. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This means it's hard to tell which powertools version is inside the layer. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
For better tracking of the ARNs and the corresponding version we need to keep track which powertools version was deployed to which layer. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
To achieve that we created two components. First, we created a version tracking app which receives evnets via Event Bridge. Second, after a successful canary deployment we send the layer ARN, powertools version, and the region to this Event Bridge. | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import aws_cdk as cdk | ||
|
||
from layer.canary_stack import CanaryStack | ||
from layer.layer_stack import LayerStack | ||
|
||
app = cdk.App() | ||
|
||
POWERTOOLS_VERSION: str = app.node.try_get_context("version") | ||
SSM_PARAM_LAYER_ARN: str = "/layers/powertools-layer-arn" | ||
|
||
VERSION_TRACKING_EVENT_BUS_ARN: str = "arn:aws:events:eu-central-1:027876851704:event-bus/VersionTrackingEventBus" | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if not POWERTOOLS_VERSION: | ||
raise ValueError( | ||
"Please set the version for Powertools by passing the '--context=version:<version>' parameter to the CDK " | ||
"synth step." | ||
) | ||
|
||
LayerStack(app, "LayerStack", powertools_version=POWERTOOLS_VERSION, ssm_paramter_layer_arn=SSM_PARAM_LAYER_ARN) | ||
|
||
CanaryStack( | ||
app, | ||
"CanaryStack", | ||
powertools_version=POWERTOOLS_VERSION, | ||
ssm_paramter_layer_arn=SSM_PARAM_LAYER_ARN, | ||
version_tracking_event_bus_arn=VERSION_TRACKING_EVENT_BUS_ARN, | ||
) | ||
|
||
app.synth() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"app": "python3 app.py", | ||
"watch": { | ||
"include": [ | ||
"**" | ||
], | ||
"exclude": [ | ||
"README.md", | ||
"cdk*.json", | ||
"requirements*.txt", | ||
"source.bat", | ||
"**/__init__.py", | ||
"python/__pycache__", | ||
"tests" | ||
] | ||
}, | ||
"context": { | ||
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true, | ||
"@aws-cdk/core:stackRelativeExports": true, | ||
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true, | ||
"@aws-cdk/aws-lambda:recognizeVersionProps": true, | ||
"@aws-cdk/aws-lambda:recognizeLayerVersion": true, | ||
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true, | ||
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, | ||
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, | ||
"@aws-cdk/core:checkSecretUsage": true, | ||
"@aws-cdk/aws-iam:minimizePolicies": true, | ||
"@aws-cdk/core:validateSnapshotRemovalPolicy": true, | ||
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true, | ||
"@aws-cdk/core:target-partitions": [ | ||
"aws", | ||
"aws-cn" | ||
] | ||
} | ||
} |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.