Last active
February 11, 2021 12:34
-
-
Save faermanj/3d347eddc43f071ccddfdbb5ab7dd5d3 to your computer and use it in GitHub Desktop.
Terraform + Cloudformation Working Sample
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
#!/bin/bash | |
set -e | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
DEFAULT_ENVNAME="BCX$(whoami | awk '{ print toupper($0) }')" | |
BC_ENVNAME=${BC_ENVNAME:-$DEFAULT_ENVNAME} | |
BC_DISTBKT=$(aws cloudformation list-exports --query "Exports[?Name=='BC::${BC_ENVNAME}::DISTBKT'].Value" --output=text) | |
BC_DISTBKT_URL="s3://${BC_DISTBKT}/" | |
echo "Emptying $BC_DISTBKT_URL" | |
aws s3 rm "$BC_DISTBKT_URL" --recursive | |
echo "Destroying" | |
pushd "$DIR" | |
terraform destroy -auto-approve | |
popd | |
echo "Done" |
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
#!/bin/bash | |
# | |
# Deploys a storage tier | |
set -e | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
DEFAULT_ENVNAME="BCX$(whoami | awk '{ print toupper($0) }')" | |
BC_ENVNAME=${BC_ENVNAME:-$DEFAULT_ENVNAME} | |
echo "Deploying storage for environment $BC_ENVNAME ..." | |
sleep 3 | |
echo "Deploying storage" | |
pushd "$DIR" | |
terraform init -upgrade | |
terraform apply -var="env_name=$BC_ENVNAME" -auto-approve | |
popd | |
echo "Storage deployed" |
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
terraform { | |
required_providers { | |
aws = { | |
version = "~> 3.0" | |
} | |
} | |
} | |
provider "aws" { | |
region = "us-west-2" | |
} | |
variable "env_name" { | |
type = string | |
default = "tfenv" | |
} | |
resource "aws_cloudformation_stack" "bcstorage" { | |
name = "${var.env_name}-STORAGE" | |
parameters = { | |
EnvName = var.env_name | |
} | |
template_body = file("${path.module}/storage.yaml") | |
} |
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
Description: Sample for storage of artifacts in different services | |
Parameters: | |
EnvName: | |
Description: An environment name that is prefixed to resource names | |
Type: String | |
Default: DEVENV | |
Resources: | |
ArtifactsBucket: | |
Type: 'AWS::S3::Bucket' | |
DeletionPolicy: Retain | |
Properties: | |
PublicAccessBlockConfiguration: | |
BlockPublicAcls: true | |
BlockPublicPolicy: true | |
IgnorePublicAcls: true | |
RestrictPublicBuckets: true | |
ContainerRepository: | |
Type: 'AWS::ECR::Repository' | |
ContainerFS: | |
Type: 'AWS::EFS::FileSystem' | |
Outputs: | |
ArtifactsBucketName: | |
Value: !Ref ArtifactsBucket | |
Export: | |
Name: !Sub "BC::${EnvName}::DISTBKT" | |
ContainerRepositoryName: | |
Value: !Ref ContainerRepository | |
Export: | |
Name: !Sub "BC::${EnvName}::ECR" | |
ContainerFSId: | |
Value: !Ref ContainerFS | |
Export: | |
Name: !Sub "BC::${EnvName}::EFS" | |
ContainerFSArn: | |
Value: !GetAtt ContainerFS.Arn | |
Export: | |
Name: !Sub "BC::${EnvName}::EFSARN" | |
# Cheat Sheet | |
# Deploy using CloudFormation | |
# export ENV_NAME="CFN1" | |
# aws cloudformation deploy --template-file storage.yaml --stack-name "${ENV_NAME}-STORAGE" --parameter-overrides "EnvName=$ENV_NAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment