Skip to content

Commit 31ac304

Browse files
Timothée Aufortafreismuth-ippon
authored andcommitted
feat: bootstrap infrastructure
1 parent 0ab0b22 commit 31ac304

File tree

8 files changed

+199
-15
lines changed

8 files changed

+199
-15
lines changed

.github/workflows/deploy.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
env:
10+
AWS_REGION: eu-west-3
11+
12+
# Permission can be added at job level or workflow level
13+
permissions:
14+
id-token: write # This is required for requesting the JWT
15+
contents: read # This is required for actions/checkout
16+
17+
jobs:
18+
terraform-10-boostrap:
19+
runs-on: ubuntu-latest
20+
defaults:
21+
run:
22+
working-directory: infrastructure/10_bootstrap
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: aws-actions/configure-aws-credentials@v4
26+
with:
27+
role-to-assume: arn:aws:iam::448878779811:role/twitch-live-1710204-my-web-site
28+
role-session-name: github-ipppontech-my-web-site-to-aws-via-oidc
29+
aws-region: ${{ env.AWS_REGION }}
30+
- uses: hashicorp/setup-terraform@v3
31+
with:
32+
terraform_version: "1.9.7"
33+
terraform_wrapper: false
34+
- run: terraform fmt -check -recursive
35+
- run: terraform init -backend=false
36+
- run: terraform validate
37+
- run: terraform init
38+
- run: terraform plan -out=tfplan.out
39+
- run: terraform apply -input=false tfplan.out
40+
41+
build:
42+
needs:
43+
- terraform-10-boostrap
44+
runs-on: ubuntu-latest
45+
steps:
46+
- uses: actions/checkout@v4
47+
- name: Use Node.js LTS
48+
uses: actions/setup-node@v4
49+
with:
50+
cache: 'npm'
51+
node-version: 'lts/*'
52+
registry-url: 'https://registry.npmjs.org'
53+
- name: build
54+
run: |
55+
npm ci
56+
npm run build
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Note: at the moment, it's not possible to use variables in Terraform backend
2+
terraform {
3+
backend "s3" {
4+
bucket = "twitch-live-17102024-tf-states"
5+
key = "10_bootstrap/terraform.tfstate"
6+
region = "eu-west-3"
7+
dynamodb_table = "twitch-live-17102024-tf-states-lock"
8+
encrypt = true
9+
}
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "aws_caller_identity" "current" {}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
locals {
2+
role_name = "twitch-live-1710204-my-web-site"
3+
}
4+
5+
import {
6+
to = aws_iam_openid_connect_provider.github
7+
id = "arn:aws:iam::448878779811:oidc-provider/token.actions.githubusercontent.com"
8+
}
9+
10+
resource "aws_iam_openid_connect_provider" "github" {
11+
url = "https://token.actions.githubusercontent.com"
12+
13+
client_id_list = [
14+
"sts.amazonaws.com",
15+
]
16+
17+
thumbprint_list = ["d89e3bd43d5d909b47a18977aa9d5ce36cee184c"]
18+
}
19+
20+
import {
21+
to = aws_iam_role.twitch_live
22+
id = local.role_name
23+
}
24+
25+
resource "aws_iam_role" "twitch_live" {
26+
name = local.role_name
27+
description = "Role dedicated to deploy infrastructure during the Twitch Live on October 17th 2024 with Arnaud and Timothee"
28+
assume_role_policy = data.aws_iam_policy_document.twitch_live_assume_role.json
29+
}
30+
31+
data "aws_iam_policy_document" "twitch_live_assume_role" {
32+
statement {
33+
effect = "Allow"
34+
principals {
35+
type = "Federated"
36+
identifiers = [
37+
aws_iam_openid_connect_provider.github.arn
38+
]
39+
}
40+
actions = [
41+
"sts:AssumeRoleWithWebIdentity"
42+
]
43+
condition {
44+
test = "StringEquals"
45+
variable = "token.actions.githubusercontent.com:aud"
46+
values = [
47+
"sts.amazonaws.com"
48+
]
49+
}
50+
condition {
51+
test = "StringLike"
52+
variable = "token.actions.githubusercontent.com:sub"
53+
values = [
54+
"repo:ippontech/my-web-site:*"
55+
]
56+
}
57+
}
58+
}
59+
60+
resource "aws_iam_role_policy_attachment" "cloudfront" {
61+
role = aws_iam_role.twitch_live.name
62+
policy_arn = "arn:aws:iam::aws:policy/CloudFrontFullAccess"
63+
}
64+
65+
resource "aws_iam_role_policy" "twitch_live_runner" {
66+
name = "${local.role_name}-runner"
67+
role = aws_iam_role.twitch_live.id
68+
policy = data.aws_iam_policy_document.twitch_live_runner.json
69+
}
70+
71+
data "aws_iam_policy_document" "twitch_live_runner" {
72+
statement {
73+
effect = "Allow"
74+
actions = [
75+
"s3:*"
76+
]
77+
resources = [
78+
"arn:aws:s3:::twitch-live-17102024-*"
79+
]
80+
}
81+
statement {
82+
effect = "Allow"
83+
actions = [
84+
"dynamodb:*"
85+
]
86+
resources = [
87+
"arn:aws:dynamodb:${var.region}:${data.aws_caller_identity.current.account_id}:table/twitch-live-17102024-tf-states-lock"
88+
]
89+
}
90+
statement {
91+
effect = "Allow"
92+
actions = [
93+
"iam:*OpenID*"
94+
]
95+
resources = [
96+
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com"
97+
]
98+
}
99+
statement {
100+
effect = "Allow"
101+
actions = [
102+
"iam:*"
103+
]
104+
resources = [
105+
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/twitch-live-1710204-my-web-site"
106+
]
107+
}
108+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
provider "aws" {
2+
region = var.region
3+
4+
default_tags {
5+
tags = {
6+
project = basename(abspath("${path.module}/../.."))
7+
subproject = basename(abspath(path.module))
8+
}
9+
}
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "region" {
2+
description = "Default AWS region"
3+
default = "eu-west-3"
4+
type = string
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = "~> 1.0"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = "~> 5.0"
7+
}
8+
}
9+
}

package-lock.json

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

0 commit comments

Comments
 (0)