Skip to content
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

[sdlf-foundations] generic deployspec.yaml #366

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdlf-cicd/template-generic-cfn-module.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AWSTemplateFormatVersion: 2010-09-09
AWSTemplateFormatVersion: "2010-09-09"
Description: Deploy a CloudFormation module

Parameters:
Expand Down
49 changes: 28 additions & 21 deletions sdlf-foundations/deployspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ deploy:
commands:
- |-
export ARTIFACTS_BUCKET=$(aws cloudformation describe-stacks --query "Stacks[?StackName=='aws-codeseeder-$SEEDFARMER_PROJECT_NAME'][].Outputs[?OutputKey=='Bucket'].OutputValue" --output text)
export DIRNAME=$(pwd)

aws s3api get-object --bucket "$ARTIFACTS_BUCKET" --key sam-translate.py sam-translate.py || {
curl -L -O https://raw.githubusercontent.com/aws/serverless-application-model/develop/bin/sam-translate.py
aws --region "$AWS_REGION" s3api put-object --bucket "$ARTIFACTS_BUCKET" --key sam-translate.py --body "$DIRNAME"/sam-translate.py
aws --region "$AWS_REGION" s3api put-object --bucket "$ARTIFACTS_BUCKET" --key sam-translate.py --body sam-translate.py
}

aws s3api get-object --bucket "$ARTIFACTS_BUCKET" --key aws-sam-cli-linux-x86_64.zip aws-sam-cli-linux-x86_64.zip || {
curl -L -O --output-dir "$DIRNAME" https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
aws --region "$AWS_REGION" s3api put-object --bucket "$ARTIFACTS_BUCKET" --key aws-sam-cli-linux-x86_64.zip --body "$DIRNAME"/aws-sam-cli-linux-x86_64.zip
curl -L -O https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip
aws --region "$AWS_REGION" s3api put-object --bucket "$ARTIFACTS_BUCKET" --key aws-sam-cli-linux-x86_64.zip --body aws-sam-cli-linux-x86_64.zip
}

aws s3api get-object --bucket "$ARTIFACTS_BUCKET" --key template-generic-cfn-module.yaml template-generic-cfn-module.yaml || {
curl -L -O https://raw.githubusercontent.com/awslabs/aws-serverless-data-lake-framework/main/sdlf-cicd/template-generic-cfn-module.yaml
aws --region "$AWS_REGION" s3api put-object --bucket "$ARTIFACTS_BUCKET" --key template-generic-cfn-module.yaml --body "$DIRNAME"/template-generic-cfn-module.yaml
aws s3api get-object --bucket "$ARTIFACTS_BUCKET" --key template-cfn-module.yaml template-cfn-module.yaml || {
curl -L https://raw.githubusercontent.com/awslabs/aws-serverless-data-lake-framework/main/sdlf-cicd/template-generic-cfn-module.yaml -o template-cfn-module.yaml
aws --region "$AWS_REGION" s3api put-object --bucket "$ARTIFACTS_BUCKET" --key template-cfn-module.yaml --body template-cfn-module.yaml
}
- |-
pip3 uninstall -y aws-sam-cli && unzip -q aws-sam-cli-linux-x86_64.zip -d sam-installation
Expand Down Expand Up @@ -51,14 +50,15 @@ deploy:
sam package --template-file ./template.yaml --s3-bucket "$ARTIFACTS_BUCKET" --s3-prefix sdlf --output-template-file template.yaml
python3 sam-translate.py --template-file=template.yaml --output-template=translated-template.json

CLOUDFORMATION_ENDPOINT_URL="https://cloudformation.$AWS_REGION.amazonaws.com"
CFN_ENDPOINT_URL="https://cloudformation.$AWS_REGION.amazonaws.com"
SSM_ENDPOINT_URL="https://ssm.$AWS_REGION.amazonaws.com"
STS_ENDPOINT_URL="https://sts.$AWS_REGION.amazonaws.com"
TEMPLATE_BASE_FILE_PATH="modules/$SEEDFARMER_PARAMETER_LIBRARY_ORG/$SEEDFARMER_PARAMETER_LIBRARY_FRAMEWORK/$SEEDFARMER_PARAMETER_LIBRARY_MODULE"
aws s3api put-object --bucket "$ARTIFACTS_BUCKET" \
--key "modules/$SEEDFARMER_PARAMETER_LIBRARY_ORG/$SEEDFARMER_PARAMETER_LIBRARY_FRAMEWORK/$SEEDFARMER_PARAMETER_LIBRARY_MODULE/translated-template.json" \
--key "$TEMPLATE_BASE_FILE_PATH/translated-template.json" \
--body translated-template.json
TEMPLATE_URL="https://$ARTIFACTS_BUCKET.s3.$AWS_REGION.amazonaws.com/modules/$SEEDFARMER_PARAMETER_LIBRARY_ORG/$SEEDFARMER_PARAMETER_LIBRARY_FRAMEWORK/$SEEDFARMER_PARAMETER_LIBRARY_MODULE/translated-template.json"
aws cloudformation --endpoint-url "$CLOUDFORMATION_ENDPOINT_URL" validate-template --template-url "$TEMPLATE_URL"
TEMPLATE_URL="https://$ARTIFACTS_BUCKET.s3.$AWS_REGION.amazonaws.com/$TEMPLATE_BASE_FILE_PATH/translated-template.json"
aws cloudformation --endpoint-url "$CFN_ENDPOINT_URL" validate-template --template-url "$TEMPLATE_URL"

mkdir module
cd module || exit
Expand All @@ -69,7 +69,7 @@ deploy:

NEW_MODULE="$(sha256sum "../$SEEDFARMER_PARAMETER_LIBRARY_MODULE.zip" | cut -c1-12)"
aws s3api put-object --bucket "$ARTIFACTS_BUCKET" \
--key "modules/$SEEDFARMER_PARAMETER_LIBRARY_ORG/$SEEDFARMER_PARAMETER_LIBRARY_FRAMEWORK/$SEEDFARMER_PARAMETER_LIBRARY_MODULE-$NEW_MODULE.zip" \
--key "$TEMPLATE_BASE_FILE_PATH-$NEW_MODULE.zip" \
--body "../$SEEDFARMER_PARAMETER_LIBRARY_MODULE.zip"

# compare hashes to avoid creating a new module version when there is no change
Expand All @@ -83,23 +83,23 @@ deploy:
fi

STACK_NAME="sdlf-cfn-module-$SEEDFARMER_PARAMETER_LIBRARY_FRAMEWORK-$SEEDFARMER_PARAMETER_LIBRARY_MODULE"
aws cloudformation --endpoint-url "$CLOUDFORMATION_ENDPOINT_URL" deploy \
aws cloudformation --endpoint-url "$CFN_ENDPOINT_URL" deploy \
--stack-name "$STACK_NAME" \
--template-file "$DIRNAME"/template-generic-cfn-module.yaml \
--template-file ./template-cfn-module.yaml \
--parameter-overrides \
pArtifactsBucket="$ARTIFACTS_BUCKET" \
pLibraryOrg="$SEEDFARMER_PARAMETER_LIBRARY_ORG" \
pLibraryFramework="$SEEDFARMER_PARAMETER_LIBRARY_FRAMEWORK" \
pLibraryModule="$SEEDFARMER_PARAMETER_LIBRARY_MODULE" \
pModuleGitRef="$NEW_MODULE" \
--tags Framework=sdlf \
--capabilities "CAPABILITY_NAMED_IAM" "CAPABILITY_AUTO_EXPAND" || exit 1
--tags Framework=sdlf || exit 1
echo "done"
cd .. && rm -Rf module
fi
- |-
if [ "$SEEDFARMER_PARAMETER_DEPLOYMENT_TYPE" = "cdk-stack" ]; then
echo "$SEEDFARMER_PARAMETER_DEPLOYMENT_TYPE"
unset SEEDFARMER_PARAMETER_DEPLOYMENT_TYPE

cat >app.py <<EOL
#!/usr/bin/env python3
Expand All @@ -116,13 +116,20 @@ deploy:
EOL

cat app.py
mapfile -t available_parameters < <(printenv -0 | awk -v RS='\0' -F= '/^SEEDFARMER_PARAMETER_/{print $1}')
STACK_PARAMETERS=()
for available_p in "${available_parameters[@]}"; do
p_without_prefix="${available_p#SEEDFARMER_PARAMETER_}"
p_without_prefix_array=( ${p_without_prefix//_/ } )
p_without_prefix_array_lc=( "${p_without_prefix_array[@],,}" )
p_without_prefix_lc=$(printf "%s" "${p_without_prefix_array_lc[@]^}")
p_cfn=$(printf -- "--parameters p%s=${!available_p}" "$p_without_prefix_lc")
STACK_PARAMETERS+=($p_cfn)
done
echo "STACK PARAMETERS: ${STACK_PARAMETERS[@]}"
cdk --no-previous-parameters \
--parameters Foundations:pOrg="$SEEDFARMER_PARAMETER_ORG" \
--parameters Foundations:pEnvironment="$SEEDFARMER_PARAMETER_ENVIRONMENT" \
--parameters Foundations:pDomain="$SEEDFARMER_PARAMETER_DOMAIN" \
--parameters Foundations:pChildAccountId="$SEEDFARMER_PARAMETER_CHILD_ACCOUNT_ID" \
"${STACK_PARAMETERS[@]}" \
deploy --require-approval never --progress events --app "python3 app.py" --outputs-file ./cdk-exports.json || exit 1
# Export metadata
seedfarmer metadata convert -f cdk-exports.json || true
fi
post_build:
Expand Down
Loading