Skip to content

01 Create an infrastructure for a terraform state #20

01 Create an infrastructure for a terraform state

01 Create an infrastructure for a terraform state #20

name: 01 Create an infrastructure for a terraform state
on:
workflow_dispatch:
env:
# prefixes must be the same as in the 00-provider.tf
AWS_BUCKET_NAME_PREFIX: "terraform-state-for-kubernetes-the-hard-way-packer"
AWS_DYNAMO_DB_TABLE_NAME_PREFIX: "terraform-state-for-terraform-state-for-kubernetes-the-hard-way-packer"
AWS_REGION: ${{ vars.AWS_REGION }}
permissions:
contents: read
jobs:
create:
name: Create a bucket and a Dynamo DB table
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Create a bucket
run: |
if [[ "${{ env.AWS_REGION }}" == "us-east-1" ]]; then
aws s3api create-bucket --bucket $AWS_BUCKET_NAME_PREFIX-$AWS_REGION --region $AWS_REGION --no-cli-pager
else
aws s3api create-bucket --bucket $AWS_BUCKET_NAME_PREFIX-$AWS_REGION --region $AWS_REGION --no-cli-pager --create-bucket-configuration LocationConstraint=$AWS_REGION
fi
aws s3api put-bucket-versioning --bucket $AWS_BUCKET_NAME_PREFIX-$AWS_REGION --versioning-configuration Status=Enabled
aws s3api put-bucket-encryption --bucket $AWS_BUCKET_NAME_PREFIX-$AWS_REGION --server-side-encryption-configuration '{"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]}'
- name: Create a DynamoDB table
run: |
aws dynamodb create-table --table-name $AWS_DYNAMO_DB_TABLE_NAME_PREFIX-$AWS_REGION --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --billing-mode PAY_PER_REQUEST --tags Key=Name,Value="terraform state dynamo table" Key=CreatedBy,Value="AWS CLI" Key=Region,Value=$AWS_REGION
- name: Create a default VPC in the region
run: |
aws ec2 create-default-vpc || true # create default VPC if not exist. It is required for AMI building