Using Terraform, How to Upload Files to S3

Terraform is an open-source, Infrastructure as Code device, created by HashiCorp. It’s a device for constructing, altering, and versioning infrastructure safely and effectively within the cloud. Infrastructure as code device permits builders to codify infrastructure in a approach that makes provisioning automated, sooner, and repeatable.

Amazon S3 is an object storage that you should utilize to retailer and retrieve any quantity of knowledge, at any time, from wherever on the internet.

On this tutorial, I’m going to indicate you the best way to add information from a laptop computer/PC to the AWS S3 bucket utilizing terraform.

Requirement

  1. AWS account and Identification Entry Administration (IAM) person with a pair of entry, secret key.
  2. Terraform put in in your system.

Step 1: Present entry key

Create a file title supplier.tf and paste the next line of code. The entry key and secret key are generated while you add a person in IAM.  Make it possible for the person has at the least the privilege of AmazonS3FullAccess. Choose the area that you will work in.

supplier “aws”

Step 2: Create a bucket

Open one other file in the identical listing title ‘s3bucket.tf’ and create our first bucket ‘b1’, title it ‘s3-terraform-bucket’. You would possibly get error if the offered title bucket ‘s3-terraform-bucket’ will not be distinctive over the worldwide AWS area. One other vital factor is ACL which gives granular entry to your bucket, make it both non-public or public. You’ll be able to present tag as your selection.

Additionally, add the file, which is positioned ‘myfiles’ listing. Outline useful resource as aws_s3_bucket_object. To discuss with the bucket you simply outline above, get it from bucket id b1. Secret’s the title given to the article as of your selection. Etag is given to search out if the file has been modified from its final add utilizing md5 sum.

# Create a bucket
useful resource “aws_s3_bucket” “b1” {

bucket = “s3-terraform-bucket”

acl = “non-public” # or might be “public-read”

tags =

}

# Add an object
useful resource “aws_s3_bucket_object” “object”

bucket = aws_s3_bucket.b1.id

key = “profile”

acl = “non-public” # or might be “public-read”

supply = “myfiles/yourfile.txt”

etag = filemd5(“myfiles/yourfile.txt”)

 

Step 2.1: To add a number of information (non-obligatory)

If you wish to add all of the information of a listing, then you could use ‘for_each’ loop.

useful resource “aws_s3_bucket_object” “object1” {
for_each = fileset(“myfiles/”, “*”)
bucket = aws_s3_bucket.b1.id
key = every.worth
supply = “myfiles/$”
etag = filemd5(“myfiles/$”)
}

Step 3: Execute

The terraform plan command is used to create an execution plan. Terraform performs a refresh, until explicitly disabled, after which determines what actions are mandatory to attain the specified state specified within the configuration information.

Lastly to execute terraform apply and see the output.

terraform plan
terraform apply

Login to your AWS console and go to S3 service. You’ll be able to see there a bucket s3-terraform -bucket and the file you add inside it.

Conclusion

We’ve got reached the top of this text. On this information, we now have walked you thru the steps required to create a bucket in AWS s3, add single or a number of information utilizing terraform.

upload folder to s3 terraform,fileset terraform,terraform s3 bucket,aws_s3_bucket_object multiple files,terraform create s3 prefix,terraform fileset,how to create a directory in terraform,archive_file,terraform lambda s3 bucket,aws_s3_bucket_object not updating,terraform archive multiple files,create a folder in s3 bucket cli,terraform create s3 bucket,terraform variables,terraform upload file to s3 example,terraform upload multiple files to s3,terraform s3,aws s3 bucket upload terraform,terraform create s3 bucket example,terraform s3 bucket folder,terraform s3 bucket logging,error putting object in s3 bucket terraform

Share: