Oluwaseun Musa's Blog


Oluwaseun Musa's Blog

How to deploy an S3 bucket via terraform

How to deploy an S3 bucket via terraform

Oluwaseun Musa's photo
Oluwaseun Musa
·Dec 12, 2022·

1 min read

# Configure the AWS provider
provider "aws" {
  access_key = "<your_access_key>"
  secret_key = "<your_secret_key>"
  region     = "<your_aws_region>"

# Create the S3 bucket
resource "aws_s3_bucket" "my_bucket" {
  bucket = "<your_bucket_name>"

In the code above, you need to replace <your_access_key>, <your_secret_key>, <your_aws_region>, and <your_bucket_name> with your own AWS access key, secret key, region, and desired bucket name, respectively.

Here is a brief explanation of what each code block does:

The provider block configures the AWS provider, which is used to interact with AWS resources. In this case, we specify the access key, secret key, and region that Terraform should use to authenticate with AWS.

The resource block creates an AWS S3 bucket. In this case, we give the bucket the name specified by <your_bucket_name>.

Once you have written the code above in a file called main.tf, you can deploy the S3 bucket by running the following commands:

# This validates and checks if your code has a spelling or configuration error
terraform validate

# Initialize the Terraform working directory
terraform init

# Preview the changes that will be made
terraform plan

# Apply the changes to create the S3 bucket
terraform apply

These commands will create the S3 bucket and output the details of the created bucket, such as its ARN (Amazon Resource Name) and URL.

I hope this helps!

Share this