Skip to content

Commit 3d9c493

Browse files
MukeshMukesh
authored andcommitted
update
1 parent 3469207 commit 3d9c493

3 files changed

Lines changed: 165 additions & 0 deletions

File tree

modules/api-gateways/main.tf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 5.0" # Allows any version 5.x.x
8+
}
9+
}
10+
}
11+
12+
# Create API Gateway
13+
resource "aws_apigatewayv2_api" "my_api" {
14+
name = "secure-api-101"
15+
protocol_type = "HTTP"
16+
}
17+
18+
# Create API Gateway Stage
19+
resource "aws_apigatewayv2_stage" "dev" {
20+
api_id = aws_apigatewayv2_api.my_api.id
21+
name = "dev"
22+
auto_deploy = true
23+
}
24+
25+
# Attach API Gateway to ALB
26+
resource "aws_apigatewayv2_integration" "alb" {
27+
api_id = aws_apigatewayv2_api.my_api.id
28+
integration_type = "HTTP_PROXY"
29+
integration_uri = var.alb_dns
30+
integration_method = "ANY"
31+
}
32+
33+
# Create Route for API Gateway
34+
resource "aws_apigatewayv2_route" "hello_route" {
35+
api_id = aws_apigatewayv2_api.my_api.id
36+
route_key = "GET /api/v1/hi"
37+
target = "integrations/${aws_apigatewayv2_integration.alb.id}"
38+
}

modules/api-gateways/variables.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "alb_dns" {
2+
type = string
3+
}

modules/vault/main.tf

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# S3 bucket for Vault storage
2+
resource "aws_s3_bucket" "vault_storage" {
3+
bucket = "vault-backend-bucket"
4+
}
5+
6+
# IAM policy for Vault to access the S3 bucket
7+
resource "aws_iam_policy" "vault_s3_policy" {
8+
name = "VaultS3Policy"
9+
description = "Allows Vault to access the S3 backend"
10+
11+
policy = <<EOF
12+
{
13+
"Version": "2012-10-17",
14+
"Statement": [
15+
{
16+
"Effect": "Allow",
17+
"Action": [
18+
"s3:PutObject",
19+
"s3:GetObject",
20+
"s3:DeleteObject",
21+
"s3:ListBucket"
22+
],
23+
"Resource": [
24+
"arn:aws:s3:::vault-backend-bucket",
25+
"arn:aws:s3:::vault-backend-bucket/*"
26+
]
27+
}
28+
]
29+
}
30+
EOF
31+
}
32+
33+
# IAM role to be assumed by Vault
34+
resource "aws_iam_role" "vault_role" {
35+
name = "vault-role"
36+
37+
assume_role_policy = <<EOF
38+
{
39+
"Version": "2012-10-17",
40+
"Statement": [
41+
{
42+
"Action": "sts:AssumeRole",
43+
"Effect": "Allow",
44+
"Principal": {
45+
"Service": "ec2.amazonaws.com"
46+
}
47+
}
48+
]
49+
}
50+
EOF
51+
}
52+
53+
# Attach the policy to the IAM role
54+
resource "aws_iam_role_policy_attachment" "vault_policy_attach" {
55+
role = aws_iam_role.vault_role.name
56+
policy_arn = aws_iam_policy.vault_s3_policy.arn
57+
}
58+
59+
# Security Group for Vault
60+
resource "aws_security_group" "vault_sg" {
61+
name = "vault-security-group"
62+
description = "Allow Vault traffic"
63+
64+
ingress {
65+
from_port = 8200
66+
to_port = 8200
67+
protocol = "tcp"
68+
cidr_blocks = ["0.0.0.0/0"]
69+
}
70+
71+
egress {
72+
from_port = 0
73+
to_port = 0
74+
protocol = "-1"
75+
cidr_blocks = ["0.0.0.0/0"]
76+
}
77+
}
78+
79+
data "aws_ami" "latest_ubuntu" {
80+
most_recent = true
81+
82+
filter {
83+
name = "name"
84+
values = ["ubuntu/images/hvm-ssd/ubuntu-*-amd64-server-*"]
85+
}
86+
87+
filter {
88+
name = "virtualization-type"
89+
values = ["hvm"]
90+
}
91+
92+
owners = ["099720109477"]
93+
}
94+
95+
output "ubuntu_ami_id" {
96+
value = data.aws_ami.latest_ubuntu.id
97+
}
98+
99+
100+
# EC2 instance for Vault
101+
resource "aws_instance" "vault" {
102+
ami = data.aws_ami.latest_ubuntu.id
103+
instance_type = "t3.medium"
104+
iam_instance_profile = aws_iam_role.vault_role.name
105+
security_groups = [aws_security_group.vault_sg.name]
106+
107+
user_data = <<-EOF
108+
#!/bin/bash
109+
sudo apt update -y
110+
sudo apt install -y unzip jq
111+
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
112+
echo "deb https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
113+
sudo apt update && sudo apt install -y vault
114+
echo "storage "s3" {
115+
bucket = \"vault-backend-bucket\"
116+
region = \"us-east-1\"
117+
}" > /etc/vault.hcl
118+
vault server -config=/etc/vault.hcl
119+
EOF
120+
121+
tags = {
122+
Name = "Vault-Server"
123+
}
124+
}

0 commit comments

Comments
 (0)