Skip to content

Commit e6ff725

Browse files
author
omerh
committed
added terraform plan
1 parent 49c94bf commit e6ff725

File tree

11 files changed

+424
-0
lines changed

11 files changed

+424
-0
lines changed

terraform/.terraform.lock.hcl

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/apprunner.tf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
resource "aws_apprunner_vpc_connector" "this" {
2+
vpc_connector_name = var.name
3+
subnets = var.subnet_list
4+
security_groups = [aws_security_group.allow_elasicache.id]
5+
}
6+
7+
resource "aws_apprunner_auto_scaling_configuration_version" "this" {
8+
auto_scaling_configuration_name = var.name
9+
10+
max_concurrency = var.max_concurrency
11+
max_size = var.max_size
12+
min_size = var.min_size
13+
14+
tags = {
15+
Name = var.name
16+
}
17+
}
18+
19+
resource "aws_apprunner_service" "this" {
20+
service_name = var.name
21+
22+
source_configuration {
23+
auto_deployments_enabled = var.auto_deployments_enabled
24+
authentication_configuration {
25+
access_role_arn = aws_iam_role.this.arn
26+
}
27+
image_repository {
28+
image_configuration {
29+
port = var.app_port
30+
runtime_environment_variables = {
31+
"REDIS_HOST" : aws_elasticache_replication_group.this.primary_endpoint_address
32+
"REDIS_PORT" : var.elasticache_port
33+
}
34+
}
35+
image_identifier = "${aws_ecr_repository.this.repository_url}:${var.app_docker_tag}"
36+
image_repository_type = var.image_repository_type
37+
}
38+
}
39+
40+
instance_configuration {
41+
cpu = var.cpu
42+
memory = var.memory
43+
}
44+
45+
health_check_configuration {
46+
healthy_threshold = var.healthy_threshold
47+
interval = var.healthcheck_interval
48+
path = var.healthcheck_path
49+
protocol = var.healthcheck_protocol
50+
timeout = var.healthcheck_timeout
51+
unhealthy_threshold = var.unhealthy_threshold
52+
}
53+
54+
network_configuration {
55+
egress_configuration {
56+
egress_type = "VPC"
57+
vpc_connector_arn = aws_apprunner_vpc_connector.this.arn
58+
}
59+
}
60+
61+
depends_on = [
62+
aws_elasticache_replication_group.this,
63+
null_resource.this
64+
]
65+
}

terraform/asg.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
resource "aws_security_group" "allow_elasicache" {
2+
name = "${var.name}-allow-elasticache"
3+
description = "Allow access to Elasticache from VPC Connector"
4+
vpc_id = var.vpc_id
5+
6+
ingress {
7+
description = "Access to Elasticache from VPC Connector"
8+
from_port = var.elasticache_port
9+
to_port = var.elasticache_port
10+
protocol = "tcp"
11+
self = true
12+
}
13+
14+
egress {
15+
from_port = 0
16+
to_port = 0
17+
protocol = "-1"
18+
cidr_blocks = ["0.0.0.0/0"]
19+
ipv6_cidr_blocks = ["::/0"]
20+
}
21+
22+
tags = {
23+
Name = "${var.name}-allow-elasticache"
24+
}
25+
}

terraform/ecr.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_ecr_repository" "this" {
2+
name = var.name
3+
image_tag_mutability = var.image_tag_mutability
4+
image_scanning_configuration {
5+
scan_on_push = var.scan_on_push
6+
}
7+
encryption_configuration {
8+
encryption_type = var.encryption_type
9+
}
10+
}

terraform/elasticache.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
resource "aws_elasticache_subnet_group" "this" {
2+
name = var.name
3+
subnet_ids = var.subnet_list
4+
}
5+
6+
resource "aws_elasticache_replication_group" "this" {
7+
engine = var.engine
8+
engine_version = var.engine_version
9+
node_type = var.node_type
10+
num_cache_clusters = var.num_cache_nodes
11+
parameter_group_name = var.parameter_group_name
12+
port = var.elasticache_port
13+
automatic_failover_enabled = var.automatic_failover_enabled
14+
replication_group_id = var.name
15+
description = var.name
16+
security_group_ids = [aws_security_group.allow_elasicache.id]
17+
subnet_group_name = aws_elasticache_subnet_group.this.name
18+
user_group_ids = []
19+
20+
apply_immediately = var.apply_immediately
21+
22+
tags = {
23+
Name = var.name
24+
}
25+
}

terraform/iam.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "aws_iam_role" "this" {
2+
name = var.name
3+
path = "/"
4+
assume_role_policy = data.aws_iam_policy_document.this.json
5+
}
6+
7+
data "aws_iam_policy_document" "this" {
8+
statement {
9+
actions = ["sts:AssumeRole"]
10+
11+
principals {
12+
type = "Service"
13+
identifiers = ["build.apprunner.amazonaws.com"]
14+
}
15+
}
16+
}
17+
18+
resource "aws_iam_role_policy_attachment" "this" {
19+
role = aws_iam_role.this.name
20+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSAppRunnerServicePolicyForECRAccess"
21+
}

terraform/localBuild.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
resource "null_resource" "this" {
4+
provisioner "local-exec" {
5+
command = <<EOT
6+
cd ..
7+
docker build -t ${aws_ecr_repository.this.repository_url}:${var.app_docker_tag} .
8+
aws ecr get-login-password --region ${var.aws_region} | docker login --username AWS --password-stdin ${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.aws_region}.amazonaws.com
9+
docker push ${aws_ecr_repository.this.repository_url}:${var.app_docker_tag}
10+
EOT
11+
interpreter = ["/bin/bash", "-c"]
12+
working_dir = path.module
13+
}
14+
depends_on = [aws_ecr_repository.this]
15+
}

terraform/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "ecr" {
2+
value = aws_ecr_repository.this.repository_url
3+
}
4+
5+
output "apprunner" {
6+
value = aws_apprunner_service.this.service_url
7+
}
8+
9+
output "elasticache" {
10+
value = aws_elasticache_replication_group.this.primary_endpoint_address
11+
}

terraform/provider.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4.5.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = var.provider_region
12+
shared_credentials_files = ["~/.aws/credentials"]
13+
profile = var.aws_profile
14+
}
15+
16+
# For pressistant S3 backend for terraform state
17+
# terraform {
18+
# backend "s3" {
19+
# bucket = "app-runner-sample"
20+
# key = "app-runner"
21+
# region = "us-east-1"
22+
# encrypt = true
23+
# profile = var.aws_profile
24+
# }
25+
# }

terraform/terraform.tfvars

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# General configuration
2+
name = "impression-counter-api"
3+
4+
# Provider configuration
5+
provider_region = "us-east-1" # Update the region
6+
aws_profile = "default" # Update the profile
7+
8+
# VPC Configurations
9+
vpc_id = "vpc-xxxxxxxxxx" # Update your VPC ID
10+
subnet_list = ["subnet-xxxxxxxx", "subnet-xxxxxxxx"] # Update your subnets
11+
12+
# Review all the default variables in variables.tfvars in

0 commit comments

Comments
 (0)