Terraform AWS S3 Bucket example

In this technical article, we will walk through an example of how to create an AWS S3 Bucket using Terraform.

prerequisites

Before getting started, you will need the following materials:

  • AWS account
  • Terraform installed
    -AWS CLI

Steps

Next, we will explain in detail the steps to create an AWS S3 Bucket using Terraform.

Step 1: Create main.tf file

Before creating the S3 Bucket, we must first create a Terraform file named main.tf.

Open your editor and type the following:

provider "aws" {
     access_key = "ACCESS-KEY"
     secret_key = "SECRET-KEY"
     region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
     bucket = "example-bucket"
     acl = "private"

     tags = {
         Name = "example-bucket"
         Environment = "test"
     }
}

In this file, we define a provider called aws, specifying the access key, key and region.

We also define a resource named aws_s3_bucket, which will create an S3 Bucket named example-bucket, with a private access control list (ACL), and tagged with name and environment.

Please note: You will need to replace ACCESS-KEY and SECRET-KEY with your AWS access key and secret.

Step 2: Initialize Terraform

Before proceeding, the Terraform project needs to be initialized.

Enter the following command at the command line:

terraform init

This will initialize Terraform and install the necessary plugins.

Step 3: Create S3 Bucket

Next, we can use Terraform to create the S3 Bucket.

Enter the following command at the command line:

terraform apply

This will create an S3 Bucket named example-bucket. Enter yes at the confirmation prompt to proceed.

Step 4: Verify Creation

Finally, we can verify that we successfully created the S3 Bucket using the AWS CLI.

Enter the following command at the command line:

aws s3 ls s3://example-bucket

If the S3 Bucket was successfully created, you'll see a list of the bucket's names.

Conclusion

Creating an AWS S3 Bucket with Terraform is very simple, just follow the above steps. Using this example, you can easily create and manage AWS S3 Buckets.

Likes(0)

Comment list count 0 Comments

No Comments

WeChat Self-Service

WeChat Consult

TaoBao

support@elephdev.com

发表
评论
Go
Top