11#! /bin/bash
22
3- # This script is run during the publish_layer workflow,
4- # and it is responsible for replacing the layer ARN in our documentation,
5- # based on the output files generated by CDK.
3+ # This script is run during the publish_layer.yml CI job,
4+ # and it is responsible for replacing the layer ARN in our documentation.
5+ # Our pipeline must generate the same layer number for all commercial regions + gov cloud
6+ # If this doesn't happens, we have an error and we must fix it in the deployment.
67#
78# see .github/workflows/publish_layer.yml
89
9- set -eo pipefail
1010
11- if [[ $# -ne 1 ]]; then
12- cat << EOM
13- Usage: $( basename $0 ) cdk-output-dir
14-
15- cdk-output-dir: directory containing the cdk output files generated when deploying the Layer
16- EOM
17- exit 1
18- fi
19-
20- CDK_OUTPUT_DIR=$1
21- DOCS_FILE=" docs/getting-started/lambda-layers.md"
22-
23- # Check if CDK output dir is a directory
24- if [ ! -d " $CDK_OUTPUT_DIR " ]; then
25- echo " No $CDK_OUTPUT_DIR directory found, not replacing lambda layer versions"
26- exit 1
11+ # Get the new version number from the first command-line argument
12+ new_version=$1
13+ if [ -z " $new_version " ]; then
14+ echo " Usage: $0 <new_version>"
15+ exit 1
2716fi
2817
29- # Process each file inside the directory
30- files=" $CDK_OUTPUT_DIR /*"
31- for file in $files ; do
32- echo " [+] Processing: $file "
33-
34- # Process each line inside the file
35- lines=$( cat " $file " )
36- for line in $lines ; do
37- echo -e " \t[*] ARN: $line "
38- # line = arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScript:49
39-
40- # From the full ARN, extract everything but the version at the end. This prefix
41- # will later be used to find/replace the ARN on the documentation file.
42- prefix=$( echo " $line " | cut -d ' :' -f 1-7)
43- # prefix = arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScript
44-
45- # Now replace the all "prefix"s in the file with the full new Layer ARN (line)
46- # prefix:\d+ ==> line
47- # sed doesn't support \d+ in a portable way, so we cheat with (:digit: :digit: *)
48- sed -i -e " s/$prefix :[[:digit:]][[:digit:]]*/$line /g" $DOCS_FILE
49-
50- # We use the eu-central-1 layer as the version for all the frameworks (SAM, CDK, SLS, etc)
51- # We could have used any other region. What's important is the version at the end.
52-
53- # Examples of strings found in the documentation with pseudo regions:
54- # arn:aws:lambda:{region}:094274105915:layer:AWSLambdaPowertoolsTypeScript:39
55- # arn:aws:lambda:${AWS::Region}:094274105915:layer:AWSLambdaPowertoolsTypeScript:39
56- # arn:aws:lambda:${aws:region}:094274105915:layer:AWSLambdaPowertoolsTypeScript:39
57- # arn:aws:lambda:{env.region}:094274105915:layer:AWSLambdaPowertoolsTypeScript:39
58- if [[ " $line " == * " eu-central-1" * ]]; then
59- # These are all the framework pseudo parameters currently found in the docs
60- for pseudo_region in ' {region}' ' ${AWS::Region}' ' ${aws:region}' ' {aws::region}' ' {env.region}' ' ${Stack.of(this).region}' ' ${aws.getRegionOutput().name}' ; do
61- prefix_pseudo_region=$( echo " $prefix " | sed " s/eu-central-1/${pseudo_region} /" )
62- # prefix_pseudo_region = arn:aws:lambda:${AWS::Region}:094274105915:layer:AWSLambdaPowertoolsTypeScript
63-
64- line_pseudo_region=$( echo " $line " | sed " s/eu-central-1/${pseudo_region} /" )
65- # line_pseudo_region = arn:aws:lambda:${AWS::Region}:094274105915:layer:AWSLambdaPowertoolsTypeScript:49
66-
67- # Replace all the "prefix_pseudo_region"'s in the file
68- # prefix_pseudo_region:\d+ ==> line_pseudo_region
69- sed -i -e " s/$prefix_pseudo_region :[[:digit:]][[:digit:]]*/$line_pseudo_region /g" $DOCS_FILE
70- done
18+ # Find all files with specified extensions in ./docs and ./examples directories
19+ # -type f: only find files (not directories)
20+ # \( ... \): group conditions
21+ # -o: logical OR
22+ # -print0: use null character as separator (handles filenames with spaces)
23+ find ./docs ./examples -type f \( -name " *.md" -o -name " *.ts" -o -name " *.yaml" -o -name " *.txt" -o -name " *.tf" -o -name " *.yml" \) -print0 | while IFS= read -r -d ' ' file; do
24+ echo " Processing file: $file "
25+
26+ # Use sed to replace the version number in the Lambda layer ARN
27+ # -i: edit files in-place without creating a backup
28+ # -E: use extended regular expressions
29+ # IF TESTING IN MAC, replace `-i` with `-i ''`
30+ # The regex matches the layer name and replaces only the version number at the end
31+ sed -i -E " s/AWSLambdaPowertoolsTypeScriptV2:[0-9]+/AWSLambdaPowertoolsTypeScriptV2:$new_version /g" " $file "
32+ if [ $? -eq 0 ]; then
33+ echo " Updated $file successfully"
34+ grep " arn:aws:lambda:" " $file "
35+ else
36+ echo " Error processing $file "
7137 fi
72- done
7338done
74-
75- echo " [+] Finished processing all commercial regions"
76-
77- # Now we need to process GovCloud regions
78- #
79- # GovCloud layers are not available in the CDK output files, but we know the ARN format and that the version is the same
80- # as the one in eu-central-1. So we can optimistically update the version of the GovCloud layers in the documentation.
81- #
82- # The GovCloud ARNs are (note that the account IDs are different in both):
83- # arn:aws-us-gov:lambda:us-gov-west-1:165093116878:layer:AWSLambdaPowertoolsTypeScriptV2:25
84- # arn:aws-us-gov:lambda:us-gov-east-1:165087284144:layer:AWSLambdaPowertoolsTypeScriptV2:25
85-
86- version=$( echo " $line " | cut -d ' :' -f 8) # version = 25
87- arn_us_gov_west_1=" arn:aws-us-gov:lambda:us-gov-west-1:165093116878:layer:AWSLambdaPowertoolsTypeScriptV2:$version "
88- prefix_us_gov_west_1=$( echo " $arn_us_gov_west_1 " | cut -d ' :' -f 1-7)
89- arn_us_gov_east_1=" arn:aws-us-gov:lambda:us-gov-east-1:165087284144:layer:AWSLambdaPowertoolsTypeScriptV2:$version "
90- prefix_us_gov_east_1=$( echo " $arn_us_gov_east_1 " | cut -d ' :' -f 1-7)
91- echo -e " \t[*] ARN GovCloud US West 1: $arn_us_gov_west_1 "
92- echo -e " \t[*] ARN GovCloud US East 1: $arn_us_gov_east_1 "
93- # Replace all the "arn_us_gov_west_1"'s in the file
94- sed -i -e " s/$prefix_us_gov_west_1 :[[:digit:]][[:digit:]]*/$arn_us_gov_west_1 /g" $DOCS_FILE
95- # Replace all the "arn_us_gov_east_1"'s in the file
96- sed -i -e " s/$prefix_us_gov_east_1 :[[:digit:]][[:digit:]]*/$arn_us_gov_east_1 /g" $DOCS_FILE
97- echo " [+] Finished processing all GovCloud regions"
98- echo " [+] Finished processing all regions"
39+ echo " Layer version update attempt completed."
0 commit comments