{"id":19496,"date":"2021-07-23T12:36:50","date_gmt":"2021-07-23T12:36:50","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=19496"},"modified":"2025-02-14T08:40:40","modified_gmt":"2025-02-14T08:40:40","slug":"aws-s3-bucket-using-terraform","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/aws-s3-bucket-using-terraform","title":{"rendered":"How To Create AWS S3 Bucket Using Terraform"},"content":{"rendered":"<h2>Tutorial Takeaway<\/h2>\n<p>In this tutorial, we will learn how to create an <b>S3 bucket<\/b> using Terraform. With that, let me get you through creating S3 bucket module in Terraform.<\/p>\n<ul class=\"bullets text-left\">\n<li>How to create an Access Key and Secret Key?<\/li>\n<li>How to create an S3 bucket with the help of Terraform modules using IAC (Infrastructure as a Code)<\/li>\n<li>How to run Terraform script in your system?<\/li>\n<\/ul>\n<h2>What is Terraform?<\/h2>\n<p>Terraform is an IAC (Infrastructure as a Code) tool allowing you to develop, update, and version infrastructure efficiently and keeping it secure. <\/p>\n<p>You can refer to the below video for exploring more about Aws-s3 bucket Terraform module<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/h970ZBgKINg\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<h2>Prerequisites<\/h2>\n<ul class=\"bullets text-left\">\n<li>An AWS IAM role with S3 permissions<\/li>\n<li>Access key ID &#038; Secret Access Key of that account.<\/li>\n<\/ul>\n<p>If you don\u2019t know how to create an access key and secret key, don\u2019t worry. We have the next section for it.<\/p>\n<h2>How to create an Access Key and Secret Key?<\/h2>\n<p>em>1. Login to your <a href=\"https:\/\/signin.aws.amazon.com\/signin?redirect_uri=https%3A%2F%2Faws.amazon.com%2Fmarketplace%2Fmanagement%2Fsignin%3Fstate%3DhashArgs%2523%26isauthcode%3Dtrue&#038;client_id=arn%3Aaws%3Aiam%3A%3A015428540659%3Auser%2Faws-mp-seller-management-portal&#038;forceMobileApp=0&#038;code_challenge=QCY-5n8K1nGf8LcWsSGfJBD83Phkwx19Myi1jJ0mqH4&#038;code_challenge_method=SHA-256\" target=\"_blank\" rel=\"noopener\">AWS Account<\/a><\/u><\/p>\n<p><u>2. Select <b>IAM<\/b><\/em><\/u><\/p>\n<p>In Management Console, search for <b>IAM<\/b>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/s3-min.jpg\" alt=\"s3 bucket\" width=\"1100\" height=\"399\" class=\"aligncenter size-full wp-image-19520\" \/><\/p>\n<p><u><em>3. On the left side of the panel, select <strong>User<\/strong>.<\/em><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/s3-dashboard-min.jpg\" alt=\"s3 dashboard\" width=\"1100\" height=\"612\" class=\"aligncenter size-full wp-image-19522\" \/><\/p>\n<p><u><em>4. Select <strong>Add Users<\/strong> and enter details.<\/em><\/u><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/S3-user-min.jpg\" alt=\"S3 bucket\" width=\"1100\" height=\"676\" class=\"aligncenter size-full wp-image-19523\" \/><\/p>\n<p>Please keep in mind to select <b>Programmatic access<\/b> in Access type to get Access Key ID and Secret Key.<\/p>\n<p><u><em>5. Attach policy<\/em><\/u><\/p>\n<p>After entering the details, attach a policy for S3 as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Inside-image-4-min.jpg\" alt=\"S3 bucket\" width=\"1100\" height=\"652\" class=\"aligncenter size-full wp-image-19524\" \/><\/p>\n<p>Select <strong>Next: Tags<\/strong> button displayed below and then <strong>Add Tags<\/strong> (optional).<\/p>\n<p><u><em>6. Create User.<\/em><\/u><\/p>\n<p>Review your user policy and then <strong>Create User<\/strong>.<\/p>\n<p>After Successful IAM user creation, you will see a message (as shown below) with your Access key and Secret key.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Inside-image-5-min.jpg\" alt=\"s3 bucket\" width=\"1100\" height=\"680\" class=\"aligncenter size-full wp-image-19525\" \/><\/p>\n<p><strong><em>Note- Store this Access Key and Secret Key to your Local Computer as AWS does not allow you to retrieve secret keys after its creation.<\/em><\/strong><\/p>\n<p>Now, moving towards how to create an <i>S3 bucket<\/i> using Terraform.<\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Looking for an expert to deploy your project or for DevOps consultation?<\/span><br \/>\nThen without a doubt, get in touch with us to work with like-minded and efficient DevOps programmers. Contact us to <a href=\"https:\/\/www.bacancytechnology.com\/hire-devops-developers\" target=\"_blank\" rel=\"noopener\">hire DevOps developer<\/a>.<\/i><\/strong><\/p>\n<h2>Steps to Create an S3 Bucket using Terraform<\/h2>\n<p>In this section, we will first discuss the S3 bucket and then the main Terraform configuration file. We will also cover the AWS S3 object bucket in terraform.<\/p>\n<h3><u>1. Create S3 bucket module<\/u><\/h3>\n<p>Create a module that will have a basic S3 file configuration. For that, create one folder named <b>\u201cS3,\u201d<\/b> we will have two files: <b>bucket.tf and var.tf<\/b>.<\/p>\n<h3><u>2. Define bucket<\/u><\/h3>\n<p>Open <em>bucket.tf<\/em> and define bucket in that.<\/p>\n<p><b>bucket.tf<\/b><\/p>\n<pre>resource \"aws_s3_bucket\" \"demos3\" {\r\n    bucket = \"${var.bucket_name}\" \r\n    acl = \"${var.acl_value}\"   \r\n}\r\n<\/pre>\n<p><u><em>Explanation<\/em><\/u><\/p>\n<ul class=\"bullets text-left\">\n<li>We have a block with the key name <em>\u201cresource\u201d<\/em> with resource type <em>\u201caws_s3_bucket\u201d<\/em>&#8211; which we want to create. It has a fixed value, and it depends on the provider. Here we have an AWS <em>S3 <\/em>resource where <em>AWS <\/em>is our provider and <em>S3 <\/em>is our resource. <em>\u201cDemos3\u201d<\/em> is the resource name that the user provides.<\/li>\n<li><em>Bucket <\/em>and <em>ACL <\/em>are the argument types for our resource. We can have different arguments according to our needs and their corresponding values.<\/li>\n<li>Either we can provide value directly or use the <em>var.tf<\/em>  file to declare the value of an argument.<\/li>\n<\/ul>\n<h3><u>3. Define variables<\/u> <\/h3>\n<p>In <em>var.tf<\/em>, we will define variables for the <em>bucket.tf<\/em><\/p>\n<p><strong>var.tf<\/strong><\/p>\n<pre>variable \"bucket_name\" {}\r\n\r\nvariable \"acl_value\" {\r\n    default = \"private\"\r\n}\r\n<\/pre>\n<p><u><em>Explanation<\/em><\/u><\/p>\n<ul class=\"bullets text-left\">\n<li>As mentioned above, <em>var.tf<\/em> is used to declare values of variables. We can either provide a default value to be used when needed or ask for value during execution.<\/li>\n<\/ul>\n<h3><u>4. Add Configuration<\/u><\/h3>\n<p>After successfully creating the S3 folder, create a file named <b>main.tf<\/b> for keeping configuration in our working directory.<\/p>\n<p><strong>main.tf<\/strong><\/p>\n<pre>provider \"aws\" {\r\n    access_key = \"${var.aws_access_key}\"\r\n    secret_key = \"${var.aws_secret_key}\"\r\n    region = \"${var.region}\"\r\n}\r\n\r\nmodule \"s3\" {\r\n    source = \"<path-to-S3-folder>\"\r\n    #bucket name should be unique\r\n    bucket_name = \"<Bucket-name>\"       \r\n}<\/pre>\n<p><u><em>Explanation<\/em><\/u><\/p>\n<ul class=\"bullets text-left\">\n<li>It contains the main set of the module\u2019s configurations.<\/li>\n<li>Here we provide details of our provider (AWS) and access key, secret key, etc.<\/li>\n<li>Since we are creating S3 using terraform modules, we need to add an S3 module to create an S3 bucket. For this, we will use the keyword <em>\u201cmodule\u201d<\/em> and the name of the module (folder) which we have created earlier.<\/li>\n<li>In argument, we will provide a source to the S3 module and bucket name, as we haven\u2019t defined bucket name in <em>var.tf.<\/em><\/li>\n<li>While writing bucket name, please keep in mind that its name is unique in the region, and it does not contain \u201c_\u201d or Uppercase letters.<\/li>\n<\/ul>\n<h3><u>5. Add Access key, Secret key, and Region.<\/u><\/h3>\n<p>Now we will define <em>variable.tf<\/em>, where we will enter our access key, secret key, and region.<\/p>\n<p><strong>variable.tf<\/strong><\/p>\n<pre>variable \"aws_access_key\" {\r\ndefault = \u201c<your_access_key>\u201d\r\n}\r\nvariable \"aws_secret_key\" {\r\ndefault = \u201c<your_secret_key>\u201d\r\n }\r\nvariable \"region\" {\r\n    default = \"region\"\r\n}<\/pre>\n<p><u><em>Explanation<\/em><\/u><\/p>\n<ul class=\"bullets text-left\">\n<li>Access key, Secret key, and Region will be defined here.<\/li>\n<\/ul>\n<p>We are done with creating the S3 bucket; now it\u2019s time to set up Terraform.<\/p>\n<h3><u>Run Terraform script in your system.<\/u><\/h3>\n<p>If you haven\u2019t downloaded terraform then visit the <a href=\"https:\/\/www.terraform.io\/downloads\" target=\"_blank\" rel=\"noopener\">Terraform official document<\/a> for downloading Terraform in your system. <\/p>\n<p>You can check the version of terraform installed in your machine using terraform -v command.<\/p>\n<p>Run the following commands to run  Terraform script in your system.<\/p>\n<p><b><em>1. terraform init<\/em><\/b><\/p>\n<p>It is used to initialize the working directory.<br \/>\nIt will install the required plugins for our code, e.g., AWS S3.<\/p>\n<p>You will see something like after running <em>terraform init<\/em> successfully-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-init-min.jpg\" alt=\"terraform init\" width=\"1100\" height=\"589\" class=\"aligncenter size-full wp-image-25143\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-init-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-init-min-300x161.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-init-min-1024x548.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-init-min-768x411.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Ever wondered how to effortlessly create an AWS S3 bucket using Terraform for seamless cloud storage?<\/span><br \/>\nLook no further! <a href=\"https:\/\/www.bacancytechnology.com\/hire-aws-developers\" target=\"_blank\" rel=\"noopener\">Hire AWS developers<\/a> who wield Terraform like magic wands. Transform your cloud experience with us &#8211; crafting S3 buckets has never been this smooth!<\/strong><\/i><\/p>\n<p><em><b>2. terraform plan<\/b><\/em><\/p>\n<p>We will use this command for script verification. It will show if there is an error in our configuration.<\/p>\n<p>The output of <em>terraform plan<\/em> looks like this if it runs successfully-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-plan-min.jpg\" alt=\"terraform plan\" width=\"1100\" height=\"594\" class=\"aligncenter size-full wp-image-25144\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-plan-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-plan-min-300x162.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-plan-min-1024x553.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-plan-min-768x415.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p><em><b>3. terraform apply<\/b><\/em><\/p>\n<p>Use <em>terraform apply<\/em> to create your S3 bucket.<\/p>\n<p>It will ask you for confirmation before execution; enter yes for confirmation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-apply.png\" alt=\"terraform apply\" width=\"1100\" height=\"594\" class=\"aligncenter size-full wp-image-25145\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-apply.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-apply-300x162.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-apply-1024x553.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/terraform-apply-768x415.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>Use terraform apply -auto-approve if you want to execute it without asking for confirmation.<\/p>\n<p>After successful execution, it will display the following message-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/successful-execution-message-min.jpg\" alt=\"successful execution message\" width=\"1100\" height=\"107\" class=\"aligncenter size-full wp-image-25146\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/successful-execution-message-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/successful-execution-message-min-300x29.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/successful-execution-message-min-1024x100.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/successful-execution-message-min-768x75.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>You can verify your bucket in S3 services in your AWS Account.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Account-snapshot-min.jpg\" alt=\"Account snapshot\" width=\"1100\" height=\"566\" class=\"aligncenter size-full wp-image-25147\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Account-snapshot-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Account-snapshot-min-300x154.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Account-snapshot-min-1024x527.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Account-snapshot-min-768x395.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>Your Bucket will be created in the desired region.<\/p>\n<p>To destroy the S3 bucket, use this command-<\/p>\n<p><strong><em>terraform destroy<\/em><\/strong><br \/>\n or<br \/>\n<strong><em>terraform destroy -auto-approve<\/em><\/strong>  \/\/ if you don\u2019t want to approve manually<\/p>\n<p>After applying terraform destroy, you will see something like this-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Destroy-Complete-min.jpg\" alt=\"Destroy Complete\" width=\"1100\" height=\"101\" class=\"aligncenter size-full wp-image-25148\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Destroy-Complete-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Destroy-Complete-min-300x28.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Destroy-Complete-min-1024x94.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/07\/Destroy-Complete-min-768x71.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>So, this was about how to create an S3 bucket using Terraform. <\/p>\n<p>You can find the source code- <a href=\"https:\/\/github.com\/AYUSHIKOUL\/s3_using_terraform\" target=\"_blank\" rel=\"noopener\">Github Repository<\/a>.<\/p>\n<h2>Conclusion<\/h2>\n<p>I hope this tutorial has served your purpose. For such advanced <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/cloud\" target=\"_blank\" rel=\"noopener\">cloud computing tutorials<\/a>, visit our Tutorials Page and learn more about emerging technologies.<\/p>\n<p>Managing DevOps needs the best and skilled experts. If you are looking for a helping hand to deploy your project or need assistance with DevOps consultation, then without a doubt, get in touch with us to work with like minded DevOps programmer. We let you hire DevOps developers from us at your ease and convenience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial Takeaway In this tutorial, we will learn how to create an S3 bucket using Terraform. With that, let me get you through creating S3 bucket module in Terraform. How to create an Access Key and Secret Key? How to create an S3 bucket with the help of Terraform modules using IAC (Infrastructure as a [&hellip;]<\/p>\n","protected":false},"author":66,"featured_media":19518,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[1262],"tags":[],"coauthors":[1758],"class_list":["post-19496","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud"],"acf":[],"modified_by":"Chandresh Patel","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/19496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=19496"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/19496\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/19518"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=19496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=19496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=19496"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=19496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}