Terraform Infrastructure as Code

Sets up Terraform on AWS for infrastructure provisioning and management

Script Author

Rowan de Haas's avatar
Rowan de Haas
Script Author

Script Details

Created 10 months ago
Updated 2 months ago
Size 6 KB

Tags

Script Content

Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#cloud-config
package_update: true
package_upgrade: true

packages:
  - wget
  - unzip
  - curl
  - git
  - gnupg
  - software-properties-common

users:
  - name: terraform
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    groups: sudo

write_files:
  - path: /home/terraform/examples/main.tf
    content: |
      terraform {
        required_version = ">= 1.0"
        required_providers {
          aws = {
            source  = "hashicorp/aws"
            version = "~> 5.0"
          }
          local = {
            source  = "hashicorp/local"
            version = "~> 2.0"
          }
        }
      }
      
      # Configure the AWS Provider
      provider "aws" {
        region = var.aws_region
      }
      
      # Variables
      variable "aws_region" {
        description = "AWS region"
        type        = string
        default     = "us-west-2"
      }
      
      variable "instance_type" {
        description = "EC2 instance type"
        type        = string
        default     = "t3.micro"
      }
      
      # Data sources
      data "aws_ami" "ubuntu" {
        most_recent = true
        owners      = ["099720109477"] # Canonical
        
        filter {
          name   = "name"
          values = ["ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server-*"]
        }
      }
      
      # Resources
      resource "aws_vpc" "main" {
        cidr_block           = "10.0.0.0/16"
        enable_dns_hostnames = true
        enable_dns_support   = true
        
        tags = {
          Name = "terraform-vpc"
        }
      }
      
      resource "aws_subnet" "public" {
        vpc_id                  = aws_vpc.main.id
        cidr_block              = "10.0.1.0/24"
        availability_zone       = data.aws_availability_zones.available.names[0]
        map_public_ip_on_launch = true
        
        tags = {
          Name = "terraform-public-subnet"
        }
      }
      
      resource "aws_internet_gateway" "main" {
        vpc_id = aws_vpc.main.id
        
        tags = {
          Name = "terraform-igw"
        }
      }
      
      resource "aws_route_table" "public" {
        vpc_id = aws_vpc.main.id
        
        route {
          cidr_block = "0.0.0.0/0"
          gateway_id = aws_internet_gateway.main.id
        }
        
        tags = {
          Name = "terraform-public-rt"
        }
      }
      
      resource "aws_route_table_association" "public" {
        subnet_id      = aws_subnet.public.id
        route_table_id = aws_route_table.public.id
      }
      
      # Outputs
      output "vpc_id" {
        description = "VPC ID"
        value       = aws_vpc.main.id
      }
      
      output "subnet_id" {
        description = "Public subnet ID"
        value       = aws_subnet.public.id
      }
    owner: terraform:terraform
    permissions: '0644'

  - path: /home/terraform/examples/versions.tf
    content: |
      terraform {
        required_version = ">= 1.0"
      }
      
      data "aws_availability_zones" "available" {
        state = "available"
      }
    owner: terraform:terraform
    permissions: '0644'

  - path: /home/terraform/.terraformrc
    content: |
      plugin_cache_dir   = "$HOME/.terraform.d/plugin-cache"
      disable_checkpoint = true
    owner: terraform:terraform
    permissions: '0644'

  - path: /etc/bash_completion.d/terraform
    content: |
      complete -C /usr/local/bin/terraform terraform
    permissions: '0644'

runcmd:
  # Install Terraform
  - cd /tmp
  - wget https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip
  - unzip terraform_1.6.0_linux_amd64.zip
  - mv terraform /usr/local/bin/
  - chmod +x /usr/local/bin/terraform
  
  # Install AWS CLI
  - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
  - unzip awscliv2.zip
  - ./aws/install
  
  # Create directories
  - mkdir -p /home/terraform/.terraform.d/plugin-cache
  - mkdir -p /home/terraform/examples
  - mkdir -p /home/terraform/modules
  - chown -R terraform:terraform /home/terraform
  
  # Install Terraform docs generator
  - wget https://github.com/terraform-docs/terraform-docs/releases/download/v0.16.0/terraform-docs-v0.16.0-linux-amd64.tar.gz
  - tar -xzf terraform-docs-v0.16.0-linux-amd64.tar.gz
  - mv terraform-docs /usr/local/bin/
  - chmod +x /usr/local/bin/terraform-docs
  
  # Install tflint
  - curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
  
  # Setup environment
  - echo 'export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"' >> /home/terraform/.bashrc
  - echo 'export TF_CLI_CONFIG_FILE="$HOME/.terraformrc"' >> /home/terraform/.bashrc
  - echo 'alias tf=terraform' >> /home/terraform/.bashrc
  
  # Initialize example configuration
  - cd /home/terraform/examples
  - sudo -u terraform terraform init
  - sudo -u terraform terraform validate
  
  # Display versions and help
  - terraform version
  - aws --version
  - terraform-docs version
  - tflint --version
  - echo "Terraform installation complete!"
  - echo "Example configuration available in /home/terraform/examples/"
  - echo "Run 'terraform plan' to see what would be created"

How to Use This Script

Cloud Provider Examples

Amazon EC2

aws ec2 run-instances
  --image-id ami-12345678
  --instance-type t3.micro
  --user-data file://script.yaml

DigitalOcean

doctl compute droplet create
  --image ubuntu-22-04-x64
  --size s-1vcpu-1gb
  --user-data-file script.yaml
  my-droplet

Google Cloud

gcloud compute instances create
  my-instance
  --metadata-from-file
  user-data=script.yaml