Skip to content

Commit f6268ad

Browse files
authored
Merge pull request #2 from optiop/feature/ops-repository
Feature/ops repository
2 parents 036014d + 623422d commit f6268ad

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ override.tf.json
3232
# Ignore CLI configuration files
3333
.terraformrc
3434
terraform.rc
35+
.terraform.lock.hcl

ops/repository/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Repository Infrastructure
2+
3+
This part contains the registry of the repositories used for the project,
4+
as well as the access roles and permission for Github actions.
5+
6+
7+
## Usage
8+
Update the `terraform.tfvars` file with the required values,
9+
set proper backend configuration in `provider.tf` file
10+
and run the following commands to create the infrastructure.
11+
12+
```bash
13+
terraform init
14+
terraform apply -var-file=terraform.tfvars
15+
```

ops/repository/ecr.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "aws_ecr_repository" "postgres" {
2+
name = "${var.name}-postgres-repo"
3+
image_tag_mutability = "MUTABLE"
4+
image_scanning_configuration {
5+
scan_on_push = true
6+
}
7+
}
8+
9+
resource "aws_ecr_repository" "grafana" {
10+
name = "${var.name}-grafana-repo"
11+
image_tag_mutability = "MUTABLE"
12+
image_scanning_configuration {
13+
scan_on_push = true
14+
}
15+
}

ops/repository/iam.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
resource "aws_iam_policy" "ecr_access_policy" {
2+
name = "${var.name}-policy"
3+
description = "Policy to allow pushing images to ECR"
4+
5+
policy = jsonencode({
6+
Version = "2012-10-17",
7+
Statement = [
8+
{
9+
Effect = "Allow",
10+
Action = [
11+
"ecr:*"
12+
],
13+
Resource = [
14+
aws_ecr_repository.postgres.arn,
15+
aws_ecr_repository.grafana.arn
16+
]
17+
},
18+
{
19+
Effect = "Allow",
20+
Action = [
21+
"ecr:GetAuthorizationToken"
22+
],
23+
Resource = [
24+
"*"
25+
]
26+
}
27+
]
28+
})
29+
}
30+
31+
resource "aws_iam_role" "github_action_role" {
32+
name = "${var.name}-role"
33+
34+
assume_role_policy = jsonencode({
35+
Version = "2012-10-17"
36+
Statement = [
37+
{
38+
Action = "sts:AssumeRoleWithWebIdentity",
39+
Effect = "Allow",
40+
Principal = {
41+
Federated = var.github_oidc_provider_arn
42+
},
43+
Condition = {
44+
StringEquals = {
45+
"token.actions.githubusercontent.com:aud" = "sts.amazonaws.com",
46+
"token.actions.githubusercontent.com:sub" = [
47+
"repo:${var.github_owner}/${var.github_repo}:ref:refs/heads/main",
48+
]
49+
}
50+
}
51+
}
52+
]
53+
})
54+
}
55+
56+
resource "aws_iam_role_policy_attachment" "github_action_policy_attachment" {
57+
role = aws_iam_role.github_action_role.name
58+
policy_arn = aws_iam_policy.ecr_access_policy.arn
59+
}

ops/repository/output.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "aws_iam_role" {
2+
value = aws_iam_role.github_action_role.arn
3+
}
4+
5+
output "aws_ecr_db_repo" {
6+
value = aws_ecr_repository.postgres.repository_url
7+
}
8+
9+
output "aws_ecr_backend_repo" {
10+
value = aws_ecr_repository.grafana.repository_url
11+
}

ops/repository/provider.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = ">=0.13"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 5.0.0"
8+
}
9+
}
10+
}
11+
12+
provider "aws" {
13+
region = var.region
14+
}

ops/repository/terraform.tfvars

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name = "postgres-grafana-on-ecs"
2+
github_owner = "optiop"
3+
github_repo = "postgres-grafana-on-ecs"
4+
github_oidc_provider_arn =
5+
region =
6+
tags = {
7+
"project" = "Visualize Postgres data on Grafana running on ECS"
8+
}

ops/repository/variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "region" {
2+
type = string
3+
}
4+
5+
variable "name" {
6+
type = string
7+
}
8+
9+
variable "tags" {
10+
type = map(string)
11+
}
12+
13+
variable "github_owner" {
14+
type = string
15+
}
16+
17+
variable "github_repo" {
18+
type = string
19+
}
20+
21+
variable "github_oidc_provider_arn" {
22+
type = string
23+
}

0 commit comments

Comments
 (0)