Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
690b1c6
another commit
shaanu22 Feb 4, 2022
01837f7
Create terraform-s3-bucket.yml
shaanu22 Feb 4, 2022
b1c3c2d
Update terraform-s3-bucket.yml
shaanu22 Feb 4, 2022
621e800
format edits
shaanu22 Feb 4, 2022
7d3acf8
Merge branch 'flugel-branch' of https://github.com/shaanu22/flugelitp…
shaanu22 Feb 4, 2022
660b2c7
Create terraform.yml
shaanu22 Feb 4, 2022
cb827c2
a few changes
shaanu22 Feb 4, 2022
105464e
Merge branch 'flugel-branch' of https://github.com/shaanu22/flugelitp…
shaanu22 Feb 4, 2022
06ed735
Update terraform.yml
shaanu22 Feb 4, 2022
c7554c5
another little edit
shaanu22 Feb 4, 2022
e0b5dce
Merge branch 'flugel-branch' of https://github.com/shaanu22/flugelitp…
shaanu22 Feb 4, 2022
58978be
Delete terraform-s3-bucket.yml
shaanu22 Feb 4, 2022
f1f4423
added go folder
shaanu22 Feb 4, 2022
1bdb721
Merge branch 'flugel-branch' of https://github.com/shaanu22/flugelitp…
shaanu22 Feb 4, 2022
7ec865a
Create terraform-s3.yml
shaanu22 Feb 4, 2022
c8d1934
Delete terraform-s3.yml
shaanu22 Feb 4, 2022
3900e18
sample
shaanu22 Feb 4, 2022
d7648da
Merge branch 'flugel-branch' of https://github.com/shaanu22/flugelitp…
shaanu22 Feb 4, 2022
000c397
edit
shaanu22 Feb 4, 2022
178eb50
Delete hello.yml
shaanu22 Feb 4, 2022
13fc736
commit edit
shaanu22 Feb 4, 2022
40ce95e
Merge branch 'flugel-branch' of https://github.com/shaanu22/flugelitp…
shaanu22 Feb 4, 2022
8f81dce
Update README.md
shaanu22 Feb 4, 2022
4281c22
Update README.md
shaanu22 Feb 4, 2022
4337cc0
Update terraform.yml
shaanu22 Feb 4, 2022
af738ad
Update terraform.yml
shaanu22 Feb 4, 2022
ccfa116
Update terraform.yml
shaanu22 Feb 4, 2022
3847ec6
Update terraform.yml
shaanu22 Feb 4, 2022
6004b39
timestamp addition
shaanu22 Feb 4, 2022
6fe0754
Merge branch 'flugel-branch' of https://github.com/shaanu22/flugelitp…
shaanu22 Feb 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: 'Terraform S3 Pipeline'

on:
push:
branches:
- flugel-branch
pull_request:
branches:
- flugel-branch

jobs:
terraform:
name: 'Terraform-S3-Bucket'
runs-on: ubuntu-latest
environment: development
steps:
- name: 'Checkout'
uses: actions/checkout@master
- name: 'Terraform Init'
uses: hashicorp/terraform-github-actions@master
with:
tf_actions_version: 1.1.4
tf_actions_subcommand: 'init'
tf_actions_working_dir: '.'
tf_actions_comment: true
args: '-var="env=dev"'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

68 changes: 67 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
This terraform module creates an S3 bucket, uploads files onto the bucket, and creates timestamps of the time of execution of the terraform configuration.# flugelitproject
**FLUGEL.IT TASK - S3 BUCKET/OBJECT CREATION AND VALIDATION**

This repository contains Terraform modules that deploy resources in AWS to demonstrate how you can use Terratest to write automated tests for your AWS Terraform code. It fulfills the following requirements:

1. Create Terraform code to create an AWS S3 bucket with two files: test1.txt and test2.txt. The content of these files must be the timestamp when the code was executed.

2. Using Terratest, create the test automation for the Terraform code, validating that both files and the bucket are created successfully.

3. Setup GitHub Actions to run a pipeline to validate this code.

4. Publish your code in a public GitHub repository, and share a Pull Request with your code. Do not merge into master until the PR is approved.

5. Include documentation describing the steps to run and test the automation.<br>


<br/>**STEPS TO ACCOMPLISH TASKS**
The following steps are needed to complete the tasks listed above: <br>


<br/>**AWS ACCOUNT CREATION**
1. Create a free-tier account with AWS.

2. As a security best practice, create an IAM user with administrative privilege for S3 bucket.

3. Download your AWS account's keypair.

4. Install AWS Command Line Interface (CLI) on your computer.

5. Globally configure your AWS credentials by running "aws configure" (on your command line) and entering your AWS credentials, OR set your environment variables with your credentials.<br>


<br/>**TERRAFORM CONFIGURATION FOR AWS BUCKET AND OBJECTS**
1. Configure your AWS credentials using one of the supported methods for AWS CLI tools, such as setting the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables. If you are using the ~/.aws/config file for profiles then export AWS_SDK_LOAD_CONFIG as "True".

2. You can set the AWS region you want to use as the environment variable AWS_DEFAULT_REGION.

3. Install Terraform and make sure it is on your PATH.

4. Run terraform init.

5. Run terraform apply.

6. When you are done creating your resources, run terraform destroy.<br>



<br/>**VALIDATING OUR TERRAFORM CODE USING TERRATEST**
Terratest runs all your terraform code in the same steps as those done by terraform. These include running terraform init, terraform apply, reading the output variable using terraform output, checking to ensure that its value is what we expect, and running terraform destroy (using defer to run it at the end of the test, whether the test succeeds or fails). However, to get to this point, do the following:

1. Install Golang and make sure your code is checked out into your GOPATH.

2. cd into the folder containing your terratest code.

3. Run "go mod init <your github.com/<YOUR_USERNAME>/<YOUR_REPO_NAME>".
Note: This step assumes you have created a Github account and a repository dedicated to your code.

4. Run "go get <packages required to run your test>".

5. Run "go mod tidy".

6. Run "go test -v" in the folder containing your terratest file.<br>


<br/>**SETTING UP GITHUB ACTION**
Based on your GitHub Workflow file content, Github Actions may require your AWS credentials, including region, access key ID, and secret access key. The values you provide for these keys will be the same as your AWS credentials.

Your GitHub Workflow file may contain actions for the following - validate, init, format, plan, apply, and so on. It depends on the events and actions that you state in the file.
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ output "s3_bucket_name" {


output "s3_bucket_region" {
value = aws_s3_bucket.terra-s3-bucket.region
value = aws_s3_bucket.terra-s3-bucket.region
}


Expand Down
6 changes: 4 additions & 2 deletions provider.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Specify the provider and access details
provider "aws" {
region = "${var.aws_region}"
}
region = var.aws_region
access_key = var.AWS_ACCESS_KEY_ID
secret_key = var.AWS_SECRET_ACCESS_KEY
}
22 changes: 11 additions & 11 deletions s3resources.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
# Create an S3 bucket, naming it with a prefix of your own choosing
resource "aws_s3_bucket" "terra-s3-bucket" {
bucket_prefix = var.bucket_prefix
acl = var.acl
acl = var.acl

# Enable versioning of the bucket to be created
versioning {
enabled = var.versioning
# Enable versioning of the bucket to be created
versioning {
enabled = var.versioning
}
# You can choose to tag the bucket
# You can choose to tag the bucket
tags = var.tags
}

# Upload multiple objects into the newly created S3 bucket
resource "aws_s3_bucket_object" "multiobject" {
bucket = aws_s3_bucket.terra-s3-bucket.id
for_each = fileset("C:\\go\\src\\flugel-terraform-s3\\test-file","*")
bucket = aws_s3_bucket.terra-s3-bucket.id
for_each = fileset("C:\\go\\src\\flugel-terraform-s3\\test-file", "*")
key = each.value
source = "C:\\go\\src\\flugel-terraform-s3\\test-file\\${each.value}"
etag = filemd5("C:\\go\\src\\flugel-terraform-s3\\test-file\\${each.value}")
etag = filemd5("C:\\go\\src\\flugel-terraform-s3\\test-file\\${each.value}")



provisioner "local-exec" {
command = "echo ${timestamp()} > C:\\go\\src\\flugel-terraform-s3\\test-file\\test1.txt"
}
Expand All @@ -30,5 +30,5 @@ resource "aws_s3_bucket_object" "multiobject" {
}

}


2 changes: 1 addition & 1 deletion test-file/test1.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2022-02-04T02:35:10Z
2022-02-04T18:01:18Z
2 changes: 1 addition & 1 deletion test-file/test2.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2022-02-04T02:35:10Z
2022-02-04T18:01:18Z
55 changes: 55 additions & 0 deletions test/flugel-terraform-s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test

import (
"fmt"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)

// An example of how to test the Terraform module in /flugel-terraform-s3 using Terratest.
func TestFlugelTerraformS3(t *testing.T) {
t.Parallel()

// Give this S3 Bucket a unique ID for a name tag so we can distinguish it from any other Buckets provisioned in your AWS account
expectedName := fmt.Sprintf("terra-s3bucket-%s", strings.ToLower(random.UniqueId()))

// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := aws.GetRandomStableRegion(t, nil, nil)

// Construct the terraform options with default retryable errors to handle the most common retryable errors in
// terraform testing.
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../../src/flugel-terraform-s3",

// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"bucket_prefix": expectedName,
"aws_region": awsRegion,
},

EnvVars: map[string]string{
"AWS_DEFAULT_REGION": awsRegion,
},
})

// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)

// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)

// Run `terraform output` to get the value of an output variable
bucketID := terraform.Output(t, terraformOptions, "s3_bucket_name")

// Verify that our Bucket has versioning enabled
actualStatus := aws.GetS3BucketVersioning(t, awsRegion, bucketID)
expectedStatus := "Enabled"
assert.Equal(t, expectedStatus, actualStatus)

}
64 changes: 64 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module test

go 1.17

require (
cloud.google.com/go v0.83.0 // indirect
cloud.google.com/go/storage v1.10.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/aws/aws-sdk-go v1.40.56 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/gruntwork-io/go-commons v0.8.0 // indirect
github.com/gruntwork-io/terratest v0.40.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.5.9 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/klauspost/compress v1.13.0 // indirect
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/otp v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/tmccombs/hcl2json v0.3.3 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
github.com/urfave/cli v1.22.2 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.47.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading