diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..498522d --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,56 @@ +name: Deploy + +on: + push: + branches: [main] + pull_request: + branches: [main] + +env: + AWS_REGION: eu-west-3 + +# Permission can be added at job level or workflow level +permissions: + id-token: write # This is required for requesting the JWT + contents: read # This is required for actions/checkout + +jobs: + terraform-10-boostrap: + runs-on: ubuntu-latest + defaults: + run: + working-directory: infrastructure/10_bootstrap + steps: + - uses: actions/checkout@v4 + - uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::448878779811:role/twitch-live-1710204-my-web-site + role-session-name: github-ipppontech-my-web-site-to-aws-via-oidc + aws-region: ${{ env.AWS_REGION }} + - uses: hashicorp/setup-terraform@v3 + with: + terraform_version: "1.9.7" + terraform_wrapper: false + - run: terraform fmt -check -recursive + - run: terraform init -backend=false + - run: terraform validate + - run: terraform init + - run: terraform plan -out=tfplan.out + - run: terraform apply -input=false tfplan.out + + build: + needs: + - terraform-10-boostrap + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Use Node.js LTS + uses: actions/setup-node@v4 + with: + cache: 'npm' + node-version: 'lts/*' + registry-url: 'https://registry.npmjs.org' + - name: build + run: | + npm ci + npm run build diff --git a/infrastructure/10_bootstrap/backend.tf b/infrastructure/10_bootstrap/backend.tf new file mode 100644 index 0000000..c314700 --- /dev/null +++ b/infrastructure/10_bootstrap/backend.tf @@ -0,0 +1,10 @@ +# Note: at the moment, it's not possible to use variables in Terraform backend +terraform { + backend "s3" { + bucket = "twitch-live-17102024-tf-states" + key = "10_bootstrap/terraform.tfstate" + region = "eu-west-3" + dynamodb_table = "twitch-live-17102024-tf-states-lock" + encrypt = true + } +} diff --git a/infrastructure/10_bootstrap/data.tf b/infrastructure/10_bootstrap/data.tf new file mode 100644 index 0000000..8fc4b38 --- /dev/null +++ b/infrastructure/10_bootstrap/data.tf @@ -0,0 +1 @@ +data "aws_caller_identity" "current" {} diff --git a/infrastructure/10_bootstrap/github_oidc.tf b/infrastructure/10_bootstrap/github_oidc.tf new file mode 100644 index 0000000..36a2701 --- /dev/null +++ b/infrastructure/10_bootstrap/github_oidc.tf @@ -0,0 +1,108 @@ +locals { + role_name = "twitch-live-1710204-my-web-site" +} + +import { + to = aws_iam_openid_connect_provider.github + id = "arn:aws:iam::448878779811:oidc-provider/token.actions.githubusercontent.com" +} + +resource "aws_iam_openid_connect_provider" "github" { + url = "https://token.actions.githubusercontent.com" + + client_id_list = [ + "sts.amazonaws.com", + ] + + thumbprint_list = ["d89e3bd43d5d909b47a18977aa9d5ce36cee184c"] +} + +import { + to = aws_iam_role.twitch_live + id = local.role_name +} + +resource "aws_iam_role" "twitch_live" { + name = local.role_name + description = "Role dedicated to deploy infrastructure during the Twitch Live on October 17th 2024 with Arnaud and Timothee" + assume_role_policy = data.aws_iam_policy_document.twitch_live_assume_role.json +} + +data "aws_iam_policy_document" "twitch_live_assume_role" { + statement { + effect = "Allow" + principals { + type = "Federated" + identifiers = [ + aws_iam_openid_connect_provider.github.arn + ] + } + actions = [ + "sts:AssumeRoleWithWebIdentity" + ] + condition { + test = "StringEquals" + variable = "token.actions.githubusercontent.com:aud" + values = [ + "sts.amazonaws.com" + ] + } + condition { + test = "StringLike" + variable = "token.actions.githubusercontent.com:sub" + values = [ + "repo:ippontech/my-web-site:*" + ] + } + } +} + +resource "aws_iam_role_policy_attachment" "cloudfront" { + role = aws_iam_role.twitch_live.name + policy_arn = "arn:aws:iam::aws:policy/CloudFrontFullAccess" +} + +resource "aws_iam_role_policy" "twitch_live_runner" { + name = "${local.role_name}-runner" + role = aws_iam_role.twitch_live.id + policy = data.aws_iam_policy_document.twitch_live_runner.json +} + +data "aws_iam_policy_document" "twitch_live_runner" { + statement { + effect = "Allow" + actions = [ + "s3:*" + ] + resources = [ + "arn:aws:s3:::twitch-live-17102024-*" + ] + } + statement { + effect = "Allow" + actions = [ + "dynamodb:*" + ] + resources = [ + "arn:aws:dynamodb:${var.region}:${data.aws_caller_identity.current.account_id}:table/twitch-live-17102024-tf-states-lock" + ] + } + statement { + effect = "Allow" + actions = [ + "iam:*OpenID*" + ] + resources = [ + "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com" + ] + } + statement { + effect = "Allow" + actions = [ + "iam:*" + ] + resources = [ + "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/twitch-live-1710204-my-web-site" + ] + } +} diff --git a/infrastructure/10_bootstrap/providers.tf b/infrastructure/10_bootstrap/providers.tf new file mode 100644 index 0000000..b748065 --- /dev/null +++ b/infrastructure/10_bootstrap/providers.tf @@ -0,0 +1,10 @@ +provider "aws" { + region = var.region + + default_tags { + tags = { + project = basename(abspath("${path.module}/../..")) + subproject = basename(abspath(path.module)) + } + } +} diff --git a/infrastructure/10_bootstrap/variables.tf b/infrastructure/10_bootstrap/variables.tf new file mode 100644 index 0000000..d5aee15 --- /dev/null +++ b/infrastructure/10_bootstrap/variables.tf @@ -0,0 +1,5 @@ +variable "region" { + description = "Default AWS region" + default = "eu-west-3" + type = string +} diff --git a/infrastructure/10_bootstrap/versions.tf b/infrastructure/10_bootstrap/versions.tf new file mode 100644 index 0000000..a8d9277 --- /dev/null +++ b/infrastructure/10_bootstrap/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_version = "~> 1.0" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} diff --git a/package-lock.json b/package-lock.json index 807030f..f4dcc2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1838,21 +1838,6 @@ "dev": true, "license": "ISC" }, - "node_modules/fsevents": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", - "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, - "hasInstallScript": true, - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",