Skip to content

Commit 1ff6949

Browse files
committed
Source blog post 2
1 parent 4c154d2 commit 1ff6949

File tree

9 files changed

+236
-5
lines changed

9 files changed

+236
-5
lines changed

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Niek Palm
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Example code blog post deploy keyless with GitHub Actions to AWS.
2+
3+
This repository contains example code for a blog post to deploy keyless with GitHub Actions to AWS.
4+
5+
- [Are you still using keys? -
6+
Practical guide to deploy with GitHub Actions and Terraform to AWS.](https://040code.github.io/2022/12/02/oidc-part-1)
7+
- [Are you still using keys? -
8+
Deploy with GitHub Action using shared workflows to AWS](https://040code.github.io/2022/12/02/oidc-part-2)
9+

Diff for: terraform/part1/variables.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ variable "github_actions_tls_certificate" {
99
}
1010

1111
variable "principals" {
12-
type = list(string)
12+
type = list(string)
1313
default = ["arn:aws:iam::557218779171:user/niek"]
1414
}
1515

1616
variable "repo" {
1717
description = "Format, org/repo. The repo will also used to create an s3 bukcet where the / is replaced by -."
18-
type = string
19-
default = "040code/blog-oidc-github-actions-aws"
18+
type = string
19+
default = "040code/blog-oidc-github-actions-aws"
2020
}
2121

2222
variable "role" {
2323
type = object({
24-
name: string
25-
path: string
24+
name : string
25+
path : string
2626
})
2727
default = {
2828
name = "blog"

Diff for: terraform/part2/.terraform.lock.hcl

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: terraform/part2/main.tf

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
data "tls_certificate" "github_actions" {
2+
url = var.github_actions_tls_certificate
3+
}
4+
5+
resource "aws_iam_openid_connect_provider" "github_actions" {
6+
url = var.github_actions_tls_certificate
7+
client_id_list = ["sts.amazonaws.com"]
8+
thumbprint_list = data.tls_certificate.github_actions.certificates.*.sha1_fingerprint
9+
}
10+
11+
data "aws_iam_policy_document" "github_actions_trusted_identity" {
12+
13+
dynamic "statement" {
14+
for_each = length(var.principals) > 0 ? ["1"] : []
15+
content {
16+
actions = ["sts:AssumeRole"]
17+
principals {
18+
type = "AWS"
19+
identifiers = var.principals
20+
}
21+
}
22+
}
23+
24+
statement {
25+
actions = ["sts:AssumeRoleWithWebIdentity"]
26+
principals {
27+
type = "Federated"
28+
identifiers = [aws_iam_openid_connect_provider.github_actions.arn]
29+
}
30+
31+
condition {
32+
test = "ForAllValues:StringEquals"
33+
variable = "token.actions.githubusercontent.com:aud"
34+
values = ["sts.amazonaws.com", var.github_actions_tls_certificate]
35+
}
36+
37+
condition {
38+
test = "StringLike"
39+
variable = "token.actions.githubusercontent.com:sub"
40+
values = ["repo:${split("/", var.repo)[0]}*:job_workflow_ref:${var.workflow}"]
41+
}
42+
}
43+
}
44+
45+
resource "aws_iam_role" "github_actions" {
46+
name = var.role.name
47+
path = var.role.path
48+
assume_role_policy = data.aws_iam_policy_document.github_actions_trusted_identity.json
49+
}
50+
51+
resource "aws_iam_role_policy" "s3" {
52+
name = "s3-policy"
53+
role = aws_iam_role.github_actions.name
54+
policy = data.aws_iam_policy_document.s3.json
55+
}
56+
57+
data "aws_iam_policy_document" "s3" {
58+
statement {
59+
sid = "1"
60+
61+
actions = [
62+
"s3:ListBucket",
63+
"s3:GetObject",
64+
"s3:PutObject"
65+
]
66+
67+
resources = [
68+
aws_s3_bucket.blog.arn, "${aws_s3_bucket.blog.arn}*"
69+
]
70+
}
71+
}
72+
73+
resource "random_uuid" "main" {
74+
}
75+
76+
resource "aws_s3_bucket" "blog" {
77+
bucket = replace(var.repo, "/", "-")
78+
force_destroy = true
79+
}

Diff for: terraform/part2/outputs.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "role" {
2+
value = aws_iam_role.github_actions.arn
3+
}
4+
5+
output "bucket" {
6+
value = aws_s3_bucket.blog.id
7+
}

Diff for: terraform/part2/provider.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
}

Diff for: terraform/part2/variables.tf

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
variable "aws_region" {
2+
type = string
3+
default = "eu-west-1"
4+
}
5+
6+
variable "github_actions_tls_certificate" {
7+
type = string
8+
default = "https://token.actions.githubusercontent.com"
9+
}
10+
11+
variable "principals" {
12+
type = list(string)
13+
default = ["arn:aws:iam::557218779171:user/niek"]
14+
}
15+
16+
variable "repo" {
17+
description = "Format, org/repo. The repo will also used to create an s3 bukcet where the / is replaced by -."
18+
type = string
19+
default = "040code/blog-oidc-github-actions-aws"
20+
}
21+
22+
variable "workflow" {
23+
description = "Workflow allowed to run the deployment."
24+
type = string
25+
default = "040code/blog-oidc-github-actions-aws/.github/workflows/s3-template.yml@refs/heads/main"
26+
}
27+
28+
variable "role" {
29+
type = object({
30+
name : string
31+
path : string
32+
})
33+
default = {
34+
name = "blog"
35+
path = "/github-actions/"
36+
}
37+
}

Diff for: terraform/part2/versions.tf

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4.0"
6+
}
7+
tls = {
8+
source = "hashicorp/tls"
9+
version = "~> 4.0"
10+
}
11+
}
12+
required_version = ">= 1.3.0"
13+
}

0 commit comments

Comments
 (0)