|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Unless explicitly stated otherwise all files in this repository are licensed |
| 4 | +# under the Apache License Version 2.0. |
| 5 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 6 | +# Copyright 2020 Datadog, Inc. |
| 7 | + |
| 8 | +# Publish the datadog lambda layer across regions, using the AWS CLI |
| 9 | +# Usage: VERSION=5 REGIONS=us-east-1 publish_layers.sh |
| 10 | +# VERSION is required. |
| 11 | +set -e |
| 12 | + |
| 13 | +# Move into the root directory, so this script can be called from any directory |
| 14 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| 15 | +cd $DIR/.. |
| 16 | + |
| 17 | +LAYER_PATH=".layers/datadog_extension.zip" |
| 18 | +LAYER_NAME="Datadog-Extension" |
| 19 | +AVAILABLE_REGIONS=$(aws ec2 describe-regions | jq -r '.[] | .[] | .RegionName') |
| 20 | + |
| 21 | +# Check that the layer files exist |
| 22 | +if [ ! -f $LAYER_PATH ]; then |
| 23 | + echo "Could not find $LAYER_PATH." |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# Determine the target regions |
| 28 | +if [ -z "$REGIONS" ]; then |
| 29 | + echo "Region not specified, running for all available regions." |
| 30 | + REGIONS=$AVAILABLE_REGIONS |
| 31 | +else |
| 32 | + echo "Region specified: $REGIONS" |
| 33 | + if [[ ! "$AVAILABLE_REGIONS" == *"$REGIONS"* ]]; then |
| 34 | + echo "Could not find $REGIONS in available regions: $AVAILABLE_REGIONS" |
| 35 | + echo "" |
| 36 | + echo "EXITING SCRIPT." |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | +fi |
| 40 | + |
| 41 | +# Determine the target layer version |
| 42 | +if [ -z "$VERSION" ]; then |
| 43 | + echo "Layer version not specified" |
| 44 | + echo "" |
| 45 | + echo "EXITING SCRIPT." |
| 46 | + exit 1 |
| 47 | +else |
| 48 | + echo "Layer version specified: $VERSION" |
| 49 | +fi |
| 50 | + |
| 51 | +read -p "Ready to publish layer $LAYER_NAME version $VERSION to regions ${REGIONS[*]} (y/n)?" CONT |
| 52 | +if [ "$CONT" != "y" ]; then |
| 53 | + echo "Exiting" |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | + |
| 57 | +publish_layer() { |
| 58 | + region=$1 |
| 59 | + version_nbr=$(aws lambda publish-layer-version --layer-name $LAYER_NAME \ |
| 60 | + --description "Datadog Lambda Extension" \ |
| 61 | + --zip-file "fileb://$LAYER_PATH" \ |
| 62 | + --region $region | jq -r '.Version') |
| 63 | + |
| 64 | + permission=$(aws lambda add-layer-version-permission --layer-name $LAYER_NAME \ |
| 65 | + --version-number $version_nbr \ |
| 66 | + --statement-id "release-$version_nbr" \ |
| 67 | + --action lambda:GetLayerVersion --principal "*" \ |
| 68 | + --region $region) |
| 69 | + |
| 70 | + echo $version_nbr |
| 71 | +} |
| 72 | + |
| 73 | +for region in $REGIONS |
| 74 | +do |
| 75 | + echo "Starting publishing layer for region $region..." |
| 76 | + |
| 77 | + latest_version=$(aws lambda list-layer-versions --region $region --layer-name $LAYER_NAME --query 'LayerVersions[0].Version || `0`') |
| 78 | + if [ $latest_version -ge $VERSION ]; then |
| 79 | + echo "Layer $LAYER_NAME version $VERSION already exists in region $region, skipping..." |
| 80 | + continue |
| 81 | + elif [ $latest_version -lt $((VERSION-1)) ]; then |
| 82 | + read -p "WARNING: The latest version of layer $LAYER_NAME in region $region is $latest_version, publish all the missing versions including $VERSION or EXIT the script (y/n)?" CONT |
| 83 | + if [ "$CONT" != "y" ]; then |
| 84 | + echo "Exiting" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + fi |
| 88 | + |
| 89 | + while [ $latest_version -lt $VERSION ]; do |
| 90 | + latest_version=$(publish_layer $region) |
| 91 | + echo "Published version $latest_version for layer $LAYER_NAME in region $region" |
| 92 | + |
| 93 | + # This shouldn't happen unless someone manually deleted the latest version, say 28, and |
| 94 | + # then tries to republish 28 again. The published version would actually be 29, because |
| 95 | + # Lambda layers are immutable and AWS will skip deleted version and use the next number. |
| 96 | + if [ $latest_version -gt $VERSION ]; then |
| 97 | + echo "ERROR: Published version $latest_version is greater than the desired version $VERSION!" |
| 98 | + echo "Exiting" |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | + done |
| 102 | +done |
| 103 | + |
| 104 | +echo "Done !" |
0 commit comments