Skip to content

Commit

Permalink
update infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledez committed Sep 2, 2023
1 parent 85a07ed commit 2e23176
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 30 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
branches:
- main

permissions:
id-token: write
contents: read

jobs:
release:
name: build and publish
Expand Down Expand Up @@ -39,10 +43,10 @@ jobs:
CI: true

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
audience: sts.amazonaws.com
role-to-assume: arn:aws:iam:::role/net.khaledez.www-github-actions
aws-region: eu-west-2 # London

- name: Setup configuration
Expand Down
31 changes: 17 additions & 14 deletions terraform/infrastructure/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions terraform/infrastructure/config.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.6.0"
version = "~> 5.15.0"
}
}
required_version = ">= 1.1.0"
required_version = ">= 1.5.0"

cloud {
organization = "khaledez"
Expand Down
1 change: 1 addition & 0 deletions terraform/infrastructure/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ module "acm" {
domains = var.domains
domain_aliases = var.domain_aliases
route53_zone_id = data.aws_route53_zone.primary.id
version = "2.0.0"
}
65 changes: 65 additions & 0 deletions terraform/infrastructure/roles.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
locals {
github_actions_issuer_domain = "token.actions.githubusercontent.com"
}

resource "aws_iam_openid_connect_provider" "github" {
url = "https://${local.github_actions_issuer_domain}"
thumbprint_list = ["1c58a3a8518e8759bf075b76b750d4f2df264fcd"]
client_id_list = ["sts.amazonaws.com"]
tags = local.common_tags
}

data "aws_iam_policy_document" "github_provider_assume_actions" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [aws_iam_openid_connect_provider.github.arn]
}
condition {
test = "StringEquals"
variable = "${local.github_actions_issuer_domain}:aud"
values = ["sts.amazonaws.com"]
}
condition {
test = "StringLike"
variable = "${local.github_actions_issuer_domain}:sub"
values = ["repo:khaledez/khaledez.net:*"]
}
}
}

resource "aws_iam_role" "github-actions" {
name = "${var.app_name}-github-actions"
tags = local.common_tags

assume_role_policy = data.aws_iam_policy_document.github_provider_assume_actions.json

managed_policy_arns = [
"arn:aws:iam::aws:policy/CloudFrontFullAccess",
"arn:aws:iam::aws:policy/AmazonS3FullAccess",
]

inline_policy {
name = "manage-domain"
policy = jsonencode({
version = "2012-10-17"
effect = "Allow"
statement = {
action = [
"route53:GetHostedZone",
"route53:ListHostedZones",
"route53:ChangeResourceRecordSets",
"route53:ListResourceRecordSets",
"route53:GetHostedZoneCount",
"route53:ListHostedZonesByName"
]
}
resource = [data.aws_route53_zone.primary.arn]
})
}

lifecycle {
create_before_destroy = true
}
}
11 changes: 3 additions & 8 deletions terraform/infrastructure/s3-backend.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
data "aws_caller_identity" "current" {}

data "aws_iam_policy_document" "backend" {
statement {
effect = "Allow"
Expand All @@ -16,8 +14,7 @@ data "aws_iam_policy_document" "backend" {
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::427368570714:user/github",
data.aws_caller_identity.current.arn
"arn:aws:iam::427368570714:root"
]
}
}
Expand All @@ -26,11 +23,9 @@ data "aws_iam_policy_document" "backend" {
resource "aws_s3_bucket" "backend" {
bucket = var.bucket_name

tags = {
tags = merge(local.common_tags, {
Description = "Backend for terraform state"
Environment = "PROD"
App = "net.khaledez.terraform"
}
})
}

resource "aws_s3_bucket_policy" "backend" {
Expand Down
6 changes: 3 additions & 3 deletions terraform/infrastructure/variables.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
variable "domains" {
description = "Domains to apply settings for"
default = ["khaledez.net", "*.dev.khaledez.net"]
default = ["khaledez.net", "*.preview.khaledez.net"]
}

variable "domain_aliases" {
description = "Aliases for domains to be added to the certificate as SAN"
type = map(set(string))
default = {
"khaledez.net" = ["www.khaledez.net"],
"*.dev.khaledez.net" = []
"khaledez.net" = ["www.khaledez.net"],
"*.preview.khaledez.net" = []
}
}

Expand Down

0 comments on commit 2e23176

Please sign in to comment.