Skip to content

Commit

Permalink
Create tf_cloud_aws.yml (#22)
Browse files Browse the repository at this point in the history
* Create tf_cloud_aws.yml

* updates for tfcloud code

* updates for tfcloud code
  • Loading branch information
chefgs authored Jul 14, 2023
1 parent d3b223a commit 15bdbe8
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/tf_cloud_aws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
# It is reusable workflow that can be called in other workflows

name: AWS Infra Creation Using in TF Cloud

on:
workflow_call:
secrets:
TF_API_TOKEN:
required: true
push:
branches: [ "*" ]
pull_request:
branches: [ "main" ]

env:
tfcode_path: tfcloud_samples/amazon_ec2

jobs:
aws_tfc_job:
name: Create AWS Infra Using TFC

runs-on: ubuntu-latest

steps:
- name: Checkout tf code in runner environment
uses: actions/[email protected]

# Configure Terraform cloud API token, since we are using Remote backend option of Terraform cloud in AWS code
- name: Setup Terraform CLI
uses: hashicorp/[email protected]
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

# Add the AWS Creds as ENV variable in TF Cloud workspace, since the tf run happens in TF Cloud environment

# Call rest of the Terraform commands
- name: Terraform init and validate
run: |
echo `pwd`
echo "** Running Terraform Init**"
terraform init
echo "** Running Terraform Validate**"
terraform validate
working-directory: ${{ env.tfcode_path }}
- name: Terraform plan and apply
run: |
echo `pwd`
echo "** Running Terraform Plan**"
terraform plan
echo "** Running Terraform Apply**"
terraform apply -auto-approve
working-directory: ${{ env.tfcode_path }}

- name: Terraform Destroy
run: |
echo "** Running Terraform Destroy**"
terraform destroy -auto-approve
working-directory: ${{ env.tfcode_path }}
22 changes: 22 additions & 0 deletions tfcloud_samples/amazon_ec2/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions tfcloud_samples/amazon_ec2/TF_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.9 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 3.27 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 3.75.1 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_instance.app_server](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_instance_count"></a> [instance\_count](#input\_instance\_count) | n/a | `number` | `2` | no |
| <a name="input_instance_count_needed"></a> [instance\_count\_needed](#input\_instance\_count\_needed) | n/a | `string` | `"true"` | no |
| <a name="input_region"></a> [region](#input\_region) | n/a | `string` | `"us-west-2"` | no |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
19 changes: 19 additions & 0 deletions tfcloud_samples/amazon_ec2/install_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# SET UP THE REPOSITORY
# Update the apt package index and install packages to allow apt to use a repository over HTTPS:
sudo apt-get update -y
sudo apt-get remove -y docker docker-engine docker.io containerd runc
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update the apt package index, and install the latest version of Docker Engine and containerd,
# or go to the next step to install a specific version:
sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
66 changes: 66 additions & 0 deletions tfcloud_samples/amazon_ec2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Variables Block
# Common values used across the terraform code can be added as variables
# We can override the values using .tfvars files while running terraform plan/apply
variable "region" {
default = "us-west-2"
}

# Terraform Required provider Block
# In this section, we need to declare the providers and their version constraint used to create the infrastructure
# It is needed to avoid any version mismatch of the provider
# Also it is good to mention what is the required version of Terraform CLI needed for the infra creation
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}

required_version = ">= 1.0.0"

backend "remote" {
hostname = "app.terraform.io"
organization = "gsaravanan-tf"

workspaces {
name = "example-workspace"
}
}
}

# Provider block declares the provider on which the infra will be created
# For AWS, one way of doing the cred authentication is to install AWS CLI and configure it to add access_key_id and secret_access_key
provider "aws" {
profile = "default"
region = var.region
}

# Resource Block
# In this section, we will add the resources that we will be adding and managing in Cloud infra
#
resource "aws_instance" "app_server" {
# x86 AMIs with hvm Ubuntu 22.04 -> ami-03f65b8614a860c29, 20.04 -> ami-0c65adc9a5c1b5d7c. Amz Linux ami-07dfed28fcf95241c
ami = "ami-03f65b8614a860c29"
instance_type = "t2.micro"

# We can use the provisioners like user_data to run scripts that will be executed when the instance is getting created.
user_data = "./install_docker.sh > /tmp/install_docker.log"

tags = {
Name = "ExampleAppServerInstance"
}
}

# Output Block
# Here we can print the values of Infra resources that is supported
# For ex: We are printing instance_id and instance_state
output "instance_id" {
description = "ID of the EC2 instance(s)"
value = aws_instance.app_server.*.id
}

output "instance_state" {
description = "State of the EC2 instance(s)"
value = aws_instance.app_server.*.instance_state
}

0 comments on commit 15bdbe8

Please sign in to comment.