adding lab 2
This commit is contained in:
parent
29e5ba28e9
commit
f687a6e9d7
19 changed files with 84 additions and 2026 deletions
BIN
1-getting-started/img/.DS_Store
vendored
Normal file
BIN
1-getting-started/img/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
1-getting-started/img/deployed-ec2.png
Normal file
BIN
1-getting-started/img/deployed-ec2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
BIN
1-getting-started/img/tofu-apply-1.png
Normal file
BIN
1-getting-started/img/tofu-apply-1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 402 KiB |
BIN
1-getting-started/img/tofu-apply-2.png
Normal file
BIN
1-getting-started/img/tofu-apply-2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 222 KiB |
BIN
1-getting-started/img/tofu-destroy-1.png
Normal file
BIN
1-getting-started/img/tofu-destroy-1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 329 KiB |
BIN
1-getting-started/img/tofu-destroy-2.png
Normal file
BIN
1-getting-started/img/tofu-destroy-2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 667 KiB |
BIN
1-getting-started/img/tofu-plan.png
Normal file
BIN
1-getting-started/img/tofu-plan.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 395 KiB |
|
@ -1,4 +1,48 @@
|
||||||
1. Create a Stack
|
### Introduction
|
||||||
|
|
||||||
|
Welcome to lab 2. You are going to find the code here very similar to lab 1, and thats by design, we'll build upon our experience and learn how to add variables and use environment files, so that you can reuse the code in say a deployment for dev, stage and prod. This is the power of IaC and prevents you having to rebuild everything from scratch.
|
||||||
|
|
||||||
|
Now I'll assume you have your AWS account and CloudShell setup already. If not please reffer to the lab 1 [README.md](../1-getting-started/README.md).
|
||||||
|
|
||||||
|
In this example we are going to do the same and deploy a VPC with subnets and an instance with a security group attached. What's different is that this time we'll use the ```user_data``` feature of EC2 to run apache and show you a running web server. In the ```main.tf``` file it differs in the ```aws_resource``` block of code to include the command to start apache.
|
||||||
|
|
||||||
|
```terraform
|
||||||
|
resource "aws_instance" "test_ami" {
|
||||||
|
ami = "ami-029b91ed285a24a90"
|
||||||
|
instance_type = "t4g.nano"
|
||||||
|
associate_public_ip_address = true
|
||||||
|
subnet_id = module.vpc.public_subnets[0]
|
||||||
|
vpc_security_group_ids = [aws_security_group.web_server_sg_tf.id]
|
||||||
|
|
||||||
|
user_data = <<-EOF
|
||||||
|
#!/bin/bash
|
||||||
|
sudo service apache2 start
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
I've also updated ```outputs.tf``` to show the public IP address of the instance we've deployed by adding this code block:
|
||||||
|
|
||||||
|
```terraform
|
||||||
|
# Public IP of instance
|
||||||
|
out "instance_public_ip" {
|
||||||
|
description = "Show the public IP of the instance deployed"
|
||||||
|
value = aws_instance.test_ami.public_ip
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Create a Stack
|
||||||
|
|
||||||
|
Once again lets deploy the stack before editing it:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd 2-simple-example/code
|
||||||
|
tofu init
|
||||||
|
tofu plan
|
||||||
|
tofu apply
|
||||||
|
```
|
||||||
|
|
||||||
|
Answer yes
|
||||||
|
|
||||||
2. Validate
|
2. Validate
|
||||||
|
|
||||||
|
|
|
@ -4,3 +4,4 @@ data "aws_security_group" "default" {
|
||||||
name = "default"
|
name = "default"
|
||||||
vpc_id = module.vpc.vpc_id
|
vpc_id = module.vpc.vpc_id
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,8 @@ locals {
|
||||||
default_tags = merge(
|
default_tags = merge(
|
||||||
var.additional_tags,
|
var.additional_tags,
|
||||||
{
|
{
|
||||||
Maintainer = "Ric"
|
|
||||||
Owner = var.name
|
Owner = var.name
|
||||||
Environment = var.environment
|
Environment = var.environment
|
||||||
ManagedBy = "terraform"
|
ManagedBy = "tofu/terraform"
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -1,13 +1,20 @@
|
||||||
provider "aws" {
|
provider "aws" {
|
||||||
region = var.region
|
region = "eu-west-1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
resource "aws_security_group" "web_server_sg_tf" {
|
resource "aws_security_group" "web_server_sg_tf" {
|
||||||
name = "web-server-sg-tf"
|
name = "web-server-sg-tf"
|
||||||
description = "Allow HTTP to web server"
|
description = "Allow HTTP to web server"
|
||||||
vpc_id = module.vpc.vpc_id
|
vpc_id = module.vpc.vpc_id
|
||||||
|
|
||||||
|
ingress {
|
||||||
|
description = "SSH ingress"
|
||||||
|
from_port = 22
|
||||||
|
to_port = 22
|
||||||
|
protocol = "tcp"
|
||||||
|
cidr_blocks = ["0.0.0.0/0"]
|
||||||
|
}
|
||||||
|
|
||||||
ingress {
|
ingress {
|
||||||
description = "HTTP ingress"
|
description = "HTTP ingress"
|
||||||
from_port = 80
|
from_port = 80
|
||||||
|
@ -33,9 +40,14 @@ egress {
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_instance" "test_ami" {
|
resource "aws_instance" "test_ami" {
|
||||||
ami = var.ami_id
|
ami = "ami-029b91ed285a24a90"
|
||||||
instance_type = "t3.micro"
|
instance_type = "t4g.nano"
|
||||||
associate_public_ip_address = true
|
associate_public_ip_address = true
|
||||||
subnet_id = module.vpc.public_subnets[0]
|
subnet_id = module.vpc.public_subnets[0]
|
||||||
vpc_security_group_ids = [aws_security_group.web_server_sg_tf.id]
|
vpc_security_group_ids = [aws_security_group.web_server_sg_tf.id]
|
||||||
|
|
||||||
|
user_data = <<-EOF
|
||||||
|
#!/bin/bash
|
||||||
|
sudo service apache2 start
|
||||||
|
EOF
|
||||||
}
|
}
|
|
@ -25,3 +25,9 @@ output "nat_public_ips" {
|
||||||
description = "List of public Elastic IPs created for AWS NAT Gateway"
|
description = "List of public Elastic IPs created for AWS NAT Gateway"
|
||||||
value = module.vpc.nat_public_ips
|
value = module.vpc.nat_public_ips
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Public IP of instance
|
||||||
|
out "instance_public_ip" {
|
||||||
|
description = "Show the public IP of the instance deployed"
|
||||||
|
value = aws_instance.test_ami.public_ip
|
||||||
|
}
|
|
@ -1,19 +1,13 @@
|
||||||
variable "name" {
|
variable "name" {
|
||||||
description = "Solution name"
|
description = "Name of our Application"
|
||||||
type = string
|
type = string
|
||||||
default = "my-vpc"
|
default = "lab-1-app"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "environment" {
|
variable "environment" {
|
||||||
description = "Execution environment"
|
description = "The deployment environment"
|
||||||
type = string
|
type = string
|
||||||
default = "development"
|
default = "dev"
|
||||||
}
|
|
||||||
|
|
||||||
variable "region" {
|
|
||||||
description = "AWS region"
|
|
||||||
type = string
|
|
||||||
default = "eu-west-1"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "private_subnet_suffix" {
|
variable "private_subnet_suffix" {
|
||||||
|
@ -40,7 +34,3 @@ variable "additional_tags" {
|
||||||
default = {}
|
default = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "ami_id" {
|
|
||||||
description = "ami to use for example"
|
|
||||||
type = string
|
|
||||||
}
|
|
10
2-simple-example/code/versions.tf
Normal file
10
2-simple-example/code/versions.tf
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
terraform {
|
||||||
|
required_version = ">= 1.0"
|
||||||
|
|
||||||
|
required_providers {
|
||||||
|
aws = {
|
||||||
|
source = "hashicorp/aws"
|
||||||
|
version = ">= 4.66.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,19 +0,0 @@
|
||||||
# This file is maintained automatically by "tofu init".
|
|
||||||
# Manual edits may be lost in future updates.
|
|
||||||
|
|
||||||
provider "registry.opentofu.org/hashicorp/aws" {
|
|
||||||
version = "5.45.0"
|
|
||||||
hashes = [
|
|
||||||
"h1:A8MJa+VwONA4BNO5xzeleguJbrblNLnXBImHTK/qgFg=",
|
|
||||||
"zh:1d71c406aeaf4ba762eb62e4595ab9c9f8da1a2c9b74bb4277c0acfd9678ae65",
|
|
||||||
"zh:3b00b13154eadedb37bca99bf7cbd556fa9472e6900c970effa17a270ee9f721",
|
|
||||||
"zh:6f264e8b70153925ac8abfa83ebffe2c2d5a27ab5557a6b16124269b08ac2441",
|
|
||||||
"zh:80f7d552faf5c43d7dc22c6c1f7e70557b9f01c67db07abbb0330d5d3fc0e464",
|
|
||||||
"zh:863a2a2e6ae5b42fc46b209d8f2761c882d46aca481a8c49ef221d290b4fd88e",
|
|
||||||
"zh:8e3bddeb2da7e6bcfd0b0221a083778d2f7fc5cd64f55de7d8d79bd1f7378bae",
|
|
||||||
"zh:c726104e46cd743bbf240101d7975f44091d893b6e97b46070df0041779b04d2",
|
|
||||||
"zh:db73a89b462fdd6eb6f32e6ed464430a895fc2e54fb629e8b99773fc32a6a7a8",
|
|
||||||
"zh:e35179b89eba358f521ffd4546345b4d0683ca3364a9deb8f3b7b4bf60be6f02",
|
|
||||||
"zh:e7b54a0faecd34a9c73729d1d1f0cfc1b8f56bae789f95987002616f1265ce72",
|
|
||||||
]
|
|
||||||
}
|
|
3
2-simple-example/iac/env/dev.tfvars
vendored
3
2-simple-example/iac/env/dev.tfvars
vendored
|
@ -1,3 +0,0 @@
|
||||||
name = "Ric Harvey"
|
|
||||||
environment = "dev"
|
|
||||||
ami_id = "ami-0f5eb0451af853a24"
|
|
|
@ -1,9 +0,0 @@
|
||||||
{
|
|
||||||
"version": 4,
|
|
||||||
"terraform_version": "1.6.2",
|
|
||||||
"serial": 98,
|
|
||||||
"lineage": "60a035a9-3548-3c8a-f46b-288ce030177e",
|
|
||||||
"outputs": {},
|
|
||||||
"resources": [],
|
|
||||||
"check_results": null
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue