Skip to content
This repository was archived by the owner on Jul 20, 2024. It is now read-only.

Commit 35047c5

Browse files
committed
Add example
1 parent 5c37031 commit 35047c5

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

‎.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.terraform/
2+
.terraform.*
3+
*.tfstate

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ module "nat" {
4646
Now create an EC2 instance in the private subnet to verify the NAT configuration.
4747
Open the [AWS Systems Manager Session Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html), log in to the instance and make sure you have external access from the instance.
4848

49+
See also the [example](example/).
50+
4951

5052
## How it works
5153

‎example/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Example of terraform-aws-nat-instance
2+
3+
Provision the stack.
4+
5+
```console
6+
% terraform init
7+
8+
% terraform apply
9+
...
10+
Plan: 37 to add, 0 to change, 0 to destroy.
11+
12+
Do you want to perform these actions?
13+
Terraform will perform the actions described above.
14+
Only 'yes' will be accepted to approve.
15+
16+
Enter a value: yes
17+
...
18+
Apply complete! Resources: 37 added, 0 changed, 0 destroyed.
19+
```
20+
21+
Make sure you can access an instance in the private subnet.
22+
23+
```console
24+
% aws ssm start-session --region us-west-2 --target i-01d945b895167862a
25+
```
26+
27+
You can completely destroy the stack.
28+
29+
```console
30+
% terraform destroy
31+
```

‎example/example.tf

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
provider "aws" {
2+
region = "us-west-2"
3+
}
4+
5+
module "vpc" {
6+
source = "terraform-aws-modules/vpc/aws"
7+
8+
name = "example"
9+
cidr = "172.18.0.0/16"
10+
azs = ["us-west-2a", "us-west-2b", "us-west-2c"]
11+
private_subnets = ["172.18.64.0/20", "172.18.80.0/20", "172.18.96.0/20"]
12+
public_subnets = ["172.18.128.0/20", "172.18.144.0/20", "172.18.160.0/20"]
13+
enable_dns_hostnames = true
14+
}
15+
16+
module "nat" {
17+
source = "../"
18+
19+
name = "example"
20+
vpc_id = module.vpc.vpc_id
21+
public_subnet = module.vpc.public_subnets[0]
22+
private_subnets_cidr_blocks = module.vpc.private_subnets_cidr_blocks
23+
private_route_table_ids = module.vpc.private_route_table_ids
24+
}
25+
26+
# instance in the private subnet
27+
resource "aws_instance" "private_instance" {
28+
ami = data.aws_ami.amazon_linux_2.id
29+
instance_type = "t3.micro"
30+
iam_instance_profile = aws_iam_instance_profile.private_instance.name
31+
subnet_id = module.vpc.private_subnets[0]
32+
33+
tags = {
34+
Name = "Example of terraform-aws-nat-instance"
35+
}
36+
}
37+
38+
# AMI of the latest Amazon Linux 2
39+
data "aws_ami" "amazon_linux_2" {
40+
most_recent = true
41+
owners = ["amazon"]
42+
filter {
43+
name = "architecture"
44+
values = ["x86_64"]
45+
}
46+
filter {
47+
name = "root-device-type"
48+
values = ["ebs"]
49+
}
50+
filter {
51+
name = "name"
52+
values = ["amzn2-ami-hvm-*"]
53+
}
54+
filter {
55+
name = "virtualization-type"
56+
values = ["hvm"]
57+
}
58+
filter {
59+
name = "block-device-mapping.volume-type"
60+
values = ["gp2"]
61+
}
62+
}
63+
64+
resource "aws_iam_instance_profile" "private_instance" {
65+
role = aws_iam_role.private_instance.name
66+
}
67+
68+
resource "aws_iam_role" "private_instance" {
69+
assume_role_policy = <<EOF
70+
{
71+
"Version": "2012-10-17",
72+
"Statement": [
73+
{
74+
"Effect": "Allow",
75+
"Principal": {
76+
"Service": "ec2.amazonaws.com"
77+
},
78+
"Action": "sts:AssumeRole"
79+
}
80+
]
81+
}
82+
EOF
83+
}
84+
85+
resource "aws_iam_role_policy_attachment" "ssm" {
86+
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
87+
role = aws_iam_role.private_instance.name
88+
}

0 commit comments

Comments
��(0)