Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Create s3 Bucket with Terraform
Ashraf Minhaj
Ashraf Minhaj

Posted on

     

Create s3 Bucket with Terraform

Introduction

Welcome to the world of Infrastructure as Code. In this tutorial we will learn how to create an s3 bucket with terraform following just 3 Steps (2 actually)!

Step1: Setup AWS cli (ignore if already installed)

Whether it's your local machine or CI/CD platform, you will need to setup AWS-CLI there.

  1. Install -Assuming you have python installed you can just run this command on your terminal/cmd -
pip install awscli
Enter fullscreen modeExit fullscreen mode

Orpip3 if you have multiple python version installed.

Else you can just followofficial aws cli installation doc.

  1. Configure CLIRun this command on terminal -
aws configure
Enter fullscreen modeExit fullscreen mode

And paste your access key, secrete access key and region and default output format as prompted -

AWS Access Key ID: MYACCESSKEYAWS Secret Access Key: MYSECRETKEYDefault region name [us-west-2]: my-aws-regionDefault output format [None]: json
Enter fullscreen modeExit fullscreen mode

That's it. Now you can start running terraform code and start making magics.

Step2: Create s3 bucket

This is the simplest way to create an s3 bucket with terraform -

# declare cloud provider firstprovider "aws" {    region      = "your-aws-region"}# to interact with aws resourcesterraform {    required_providers {      aws = {        source  = "hashicorp/aws"        version = "~> 4.30"      }    }}# create s3 bucket named my-lovely-bucketresource "aws_s3_bucket" "demos3" {    bucket = "my-lovely-bucket"   }
Enter fullscreen modeExit fullscreen mode

You can download the code fromhere.

Step3: Apply

That's it. Now go inside the terraform directory, start a terminal in that folder and run -

terraform init

It will download necessary things along with a '.tstate' file. After than you can just apply. But it's good practice to see the possible changes first using plan then apply. So, run -

terraform plan
Enter fullscreen modeExit fullscreen mode

It will output possible changes list, make sure you go through and if you are ok just run -

terraform apply
Enter fullscreen modeExit fullscreen mode

That's it!! Your changes (creating a bucket) will be applied on aws! Go to aws console and check it for yourself.

Remember: While learning, to avoid unexpected bill please destroy resources after playing around.

To Destroy

terraform destroy

Conclusion

I hope you had fun. Best wishes for your cloudy journey ahead!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Fond of automating things, DevOps is love and AWS is the crush. Ex is Python, still likes her secretly.
  • Education
    BSc. in CSE, Britannia University
  • Work
    DevOps Engineer
  • Joined

More fromAshraf Minhaj

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp