GitHub Actionsで利用するAWS IAM RoleをTerraformで作成する
はじめに
以前投稿したGitHub Actionsの実行に向けたTerraformによるAWS IAM Roleの作成について - 深夜の怠惰な技術ブログにて、GitHub Actionsで利用するAWS IAM ProviderとIAM Roleの作成を記述しましたが、その時はthumbprint_listの部分をハードコーディングしていました。 今回のブログでは、このthumbprintの取得についてもTerraformで記述して、改めて記載していきたいと思います。
TerraformにおけるThumbPrintの取得
data resourceによるGitHubのOIDC Configurationへのリクエストと証明書の取得
該当のTerraformコードは以下のようになります。
data "http" "github_openid_configuration" { url = "https://token.actions.githubusercontent.com/.well-known/openid-configuration" } data "tls_certificate" "github" { url = jsondecode(data.http.github_openid_configuration.body).jwks_uri }
ここについてはOIDCの規格に準拠している処理となりまります。参考までに、https://token.actions.githubusercontent.com/.well-known/openid-configurationに対してcurlでリクエストを実施してみると以下のレスポンスを取得できることがわかります。
curl https://token.actions.githubusercontent.com/.well-known/openid-configuration | jq { "issuer": "https://token.actions.githubusercontent.com", "jwks_uri": "https://token.actions.githubusercontent.com/.well-known/jwks", "subject_types_supported": [ "public", "pairwise" ], "response_types_supported": [ "id_token" ], "claims_supported": [ "sub", "aud", "exp", "iat", "iss", "jti", "nbf", "ref", "repository", "repository_id", "repository_owner", "repository_owner_id", "run_id", "run_number", "run_attempt", "actor", "actor_id", "workflow", "head_ref", "base_ref", "event_name", "ref_type", "environment", "job_workflow_ref", "repository_visibility" ], "id_token_signing_alg_values_supported": [ "RS256" ], "scopes_supported": [ "openid" ] }
IAM ProviderとIAM Roleの作成
前節で記載したdata resourceを参照して、IAM Providerを作成します。該当のTerraformコードは以下のようになります。
resource "aws_iam_openid_connect_provider" "github" { client_id_list = ["sts.amazonaws.com"] thumbprint_list = [data.tls_certificate.github.certificates[0].sha1_fingerprint] url = "https://token.actions.githubusercontent.com" }
あとは、このIAM Providerを参照して、IAM Roleを作成するだけとなります。IAM Roleを作成するためのTerraformコードについては冒頭に貼付したリンクのブログにて記載済みとなりますので、ここでは割愛させていただきます。
さいごに
いかがだったでしょうか。みなさまには釈迦に説法かもしれませんが、僕自身の備忘のために記載致しました。