Git Product home page Git Product logo

basics-cloudformation's Introduction

Deployment of an S3 Bucket for Terraform State using CloudFormation

This guide provides instructions on how to create an S3 bucket using AWS CloudFormation, which will be used by Terraform to store its state file.

CloudFormation Template for S3 Bucket

The following is a basic CloudFormation template to create an S3 bucket with versioning enabled, suitable for storing Terraform state:

AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation to create an S3 bucket for Terraform state'

Resources:
  TerraformStateBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: your-unique-bucket-name
      VersioningConfiguration:
        Status: Enabled

  BucketPolicy:
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket: !Ref TerraformStateBucket
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: 'AllowSSEPut'
            Effect: 'Allow'
            Principal: '*'
            Action: 's3:PutObject'
            Resource: !Sub 'arn:aws:s3:::your-unique-bucket-name/*'
            Condition:
              StringEquals:
                's3:x-amz-server-side-encryption': 'AES256'

Replace your-unique-bucket-name with your desired bucket name.

Configuring Terraform to Use the S3 Bucket

After deploying the bucket with CloudFormation, configure Terraform to use this bucket for its remote state:

terraform {
  backend "s3" {
    bucket         = "your-unique-bucket-name"
    key            = "path/to/my/terraform.tfstate"
    region         = "your-aws-region"
    encrypt        = true
    dynamodb_table = "optional-dynamodb-lock-table-name"
    acl            = "private"
  }
}

Replace your-unique-bucket-name, your-aws-region, and path/to/my/terraform.tfstate with your specific values.

Executing the CloudFormation Template

To deploy the template:

  • Prerequisites:

    • Ensure AWS CLI is installed and configured with your credentials and default region.
  • Deploy the Template:

    • Save your template as my-s3-bucket.yaml.
    • Run the following command:
aws cloudformation create-stack --stack-name my-stack-s3 --template-body file://my-s3-bucket.yaml --region eu-central-1
  • Verify Stack Creation:
    • Use the AWS CLI or AWS Management Console to check the status of your stack.

Additional Notes

  • Error Handling: Check the AWS CloudFormation console for any errors during stack creation.
  • Stack Deletion: To delete the stack and its resources, use:
aws cloudformation delete-stack --stack-name my-stack-s3 --region eu-central-1

basics-cloudformation's People

Watchers

Fernando Piñero avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.