-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
139 lines (118 loc) · 3.83 KB
/
main.tf
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
provider "aws" {
region = "us-east-1" # Replace with your desired region
}
terraform {
backend "s3" {
bucket = "lambda-redis-test-1000"
key = "terraform.tfstate"
region = "us-east-1"
encrypt = true
}
}
# Define a VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16" # Replace with your desired CIDR block
}
# Define a public subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24" # Replace with your desired CIDR block for the public subnet
availability_zone = "us-east-1a" # Replace with your desired availability zone
map_public_ip_on_launch = true
}
# Create an Internet Gateway and attach it to the VPC
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
}
# # # Create a Route Table for public subnet
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
}
# # Associate the public subnet with the public route table
resource "aws_route_table_association" "public_subnet_association" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}
# Define a security group for the EC2 instance
resource "aws_security_group" "ec2_security_group" {
name = "ec2-security-group"
description = "Security group for the EC2 instance"
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Adjust this to your trusted IP or CIDR block
}
# # Define inbound rule for port 6379 (Redis)
ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Define an EC2 instance
resource "aws_instance" "ec2_instance" {
ami = "ami-0793d6f1bd8ddb11c" # Replace with your desired AMI ID
instance_type = "t2.micro" # Replace with your desired instance type
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.ec2_security_group.id]
key_name = "my-local-2"
tags = {
Name = "Redis Test"
}
}
# Create a null_resource to run remote-exec
resource "null_resource" "remote_exec" {
triggers = {
# You can add dependencies here if needed
instance_id = aws_instance.ec2_instance.id
}
# Use the provisioner to execute remote commands
provisioner "remote-exec" {
inline = [
"sudo service docker start",
"sudo chmod 666 /var/run/docker.sock",
"docker run -d -p 6379:6379 --name my-redis redis:alpine --requirepass ${var.REDIS_PASS}"
]
connection {
type = "ssh"
user = "ec2-user" # Replace with the SSH user for your AMI
private_key = "${file("~/.ssh/id_rsa")}" # Replace with the path to your SSH private key
host = aws_instance.ec2_instance.public_ip
}
}
}
# Define an AWS Lambda function
resource "aws_lambda_function" "my_lambda" {
function_name = "my-lambda-function"
handler = "index.handler"
runtime = "nodejs18.x" # Replace with your desired runtime
role = "arn:aws:iam::344965508130:role/my_lambda_role" # Replace with your IAM role ARN
filename = "lambda_function_${var.lambdasVersion}.zip"
environment {
variables = {
REDIS_HOST = aws_instance.ec2_instance.public_ip # Use the private IP of the EC2 instance
REDIS_PORT = 6379
REDIS_PASS = "${var.REDIS_PASS}"
REDIS_DB_PATH = "${var.REDIS_DB_PATH}"
}
}
}
variable "lambdasVersion" {
type = string
description = "version of the lambdas zip on S3"
}
variable "REDIS_DB_PATH" {
type = string
description = "Redis DB Path"
}
variable "REDIS_PASS" {
type = string
description = "Redis EC2 DB Pass"
}