Skip to content

isejalabs/bootstrap-amazon-s3-remote-backend-for-open-tofu

 
 

Repository files navigation

OpenTofu Infrastructure Bootstrap

OpenTofu is a Terraform fork, created as an initiative of Gruntwork, Spacelift, Harness, Env0, Scalr, and others, in response to HashiCorp’s switch from an open-source license to the BUSL. This project provides the necessary scripts and AWS CloudFormation templates to set up a secure and robust Amazon S3 remote backend for OpenTofu.

Table of Contents

Prerequisites

Before running the bootstrap script, ensure you have the following:

Click here to view the minimum permission which are required to execute the code
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AmazonS3",
            "Effect": "Allow",
            "Action": [
                "s3:PutEncryptionConfiguration",
                "s3:PutBucketLogging",
                "s3:PutLifecycleConfiguration",
                "s3:PutBucketPolicy",
                "s3:ListBucket",
                "s3:PutBucketVersioning",
                "cloudformation:ExecuteChangeSet",
                "s3:CreateBucket",
                "s3:GetBucketLogging",
                "s3:GetBucketPolicy",
                "s3:GetBucketPolicyStatus",
                "s3:GetBucketTagging",
                "s3:GetBucketVersioning",
                "s3:ListAllMyBuckets",
                "s3:GetBucketLocation",
                "s3:GetBucketOwnershipControls",
                "s3:GetBucketPublicAccessBlock",
                "s3:GetLifecycleConfiguration",
                "s3:GetEncryptionConfiguration",
                "s3:ListTagsForResource",
                "s3:TagResource",
                "s3:GetIntelligentTieringConfiguration",
                "s3:UntagResource",
                "s3:GetBucketAcl",
                "s3:ListAccessPoints",
                "s3:GetAccountPublicAccessBlock",
                "s3:PutBucketPublicAccessBlock"
            ],
            "Resource": "arn:aws:s3:::*"
        },
        {
            "Sid": "S3Object",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject"
            ],
            "Resource": "arn:aws:s3:::*/*"
        },
        {
            "Sid": "DynamoDB",
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:DescribeTable",
                "dynamodb:ListTables",
                "dynamodb:CreateTable",
                "dynamodb:PutItem",
                "dynamodb:TagResource",
                "dynamodb:UntagResource",
                "dynamodb:UpdateContinuousBackups",
                "dynamodb:DescribeContinuousBackups",
                "dynamodb:UpdateItem",
                "dynamodb:DeleteItem"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Sid": "KMS",
            "Effect": "Allow",
            "Action": [
                "kms:CreateKey",
                "kms:CreateAlias",
                "kms:PutKeyPolicy",
                "kms:ListAliases",
                "kms:ListKeys",
                "kms:TagResource",
                "kms:UntagResource",
                "kms:DescribeKey",
                "kms:ListGrants",
                "kms:Encrypt",
                "kms:TagResource",
                "kms:UntagResource",
                "kms:CreateKey",
                "kms:ListKeyPolicies",
                "kms:GetKeyPolicy",
                "kms:PutKeyPolicy",
                "kms:EnableKey",
                "kms:CreateGrant",
                "kms:Decrypt",
                "kms:GenerateDataKey"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Sid": "CloudFormation",
            "Effect": "Allow",
            "Action": [
                "cloudformation:DescribeStacks",
                "cloudformation:ListStacks",
                "cloudformation:CreateChangeSet",
                "cloudformation:CreateStack",
                "cloudformation:TagResource",
                "cloudformation:UntagResource",
                "cloudformation:DescribeStackEvents",
                "cloudformation:DescribeChangeSet",
                "cloudformation:ExecuteChangeSet",
                "cloudformation:GetTemplateSummary"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

Setup

Clone the repository and navigate to the bootstrap directory:

git clone https://github.com/aws-samples/bootstrap-amazon-s3-remote-backend-for-open-tofu.git

Usage

The bootstrap.sh script is used to initialize and apply the OpenTofu backend infrastructure.

Deploying the Infrastructure

To deploy the OpenTofu infrastructure, execute the subsequent command. The region argument is optional and defaults to eu-central-1:

bash bootstrap/bootstrap.sh <aws-region>

This script performs the following actions:

  • Deploys the bootstrap.yaml CloudFormation template.
  • Retrieves and prints out the S3 bucket and DynamoDB table names.
  • Initializes and applies OpenTofu backend configuration.

Understanding the Files

  • bootstrap.sh: Shell script to orchestrate the bootstrap process.
  • bootstrap.yaml: CloudFormation template that defines the required AWS resources.

CloudFormation Stack Details

The bootstrap.yaml template will create:

Resources come with retain policies to prevent accidental deletion.

Architecture

OpenTofu remote backend

  1. The user assumes an Amazon IAM role with the sufficient permissions provided above.
  2. The user deploys the OpenTofu Amazon S3 remote backend using the assumed Amazon IAM role
  3. The user deploys infrastructure using OpenTofu which stores the remote state on Amazon S3.

Outputs

After running the script, it will output the names of:

  • The Amazon S3 bucket for the OpenTofu backend.
  • The AWS DynamoDB table for the OpenTofu backend.

Security

The CloudFormation template is designed with security in mind:

  • Encryption is enforced on the Amazon S3 bucket using an Amazon KMS Key.
  • Public access is blocked for all Amazon S3 buckets.
  • Deletion policies are set to retain to prevent data loss.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

Dependencies and Licenses

This project is licensed under the MIT License - see the LICENSE file for details.

OpenTofu Project

This project uses OpenTofu as a key dependency. OpenTofu is an open-source software project licensed under the Mozilla Public License 2.0 (MPL-2.0).

OpenTofu License Information:

We adhere to the terms and conditions of the MPL-2.0 license for the OpenTofu component within our project. Please refer to the provided links for more information on OpenTofu and its license.

About

Fork of aws-sample's s3-remote-backend-for-open-tofu bootstrapping impl

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 88.3%
  • HCL 11.7%