
GitHub Actions と AWS の OIDC 連携で `No OpenIDConnect provider found in your account for https://token.actions.githubusercontent.com/ABCDE` というエラーが発生する場合の対処
こんにちは、製造ビジネステクノロジー部の若槻です。
今回は GitHub Actions との OIDC 連携用の IAM リソースを AWS 上に作成しているにも関わらず、Assume Role で No OpenIDConnect provider found in your account for https://token.actions.githubusercontent.com/ABCDE
というエラーが発生する場合の対処についてです。
事象
次のように AWS CDK で GitHub Actions が信頼する OIDC プロバイダーおよび、Assume Role で使用する IAM ロールを作成しています。
- OIDC プロバイダー
import { aws_iam } from 'aws-cdk-lib';
import { Construct } from 'constructs';
// OIDC プロバイダー
export class GitHubActionsOidcProviderConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
new aws_iam.OpenIdConnectProvider(this, 'Default', {
url: 'https://token.actions.githubusercontent.com',
clientIds: ['sts.amazonaws.com'],
});
}
}
- IAM ロール
import { aws_iam, Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
interface GitHubActionsOidcConstructProps {
gitHubOwner: string;
gitHubRepo: string;
}
// GitHub Actions が Assume Role で使用する IAM ロール
export class GitHubActionsOidcConstruct extends Construct {
constructor(
scope: Construct,
id: string,
props: GitHubActionsOidcConstructProps
) {
super(scope, id);
const { gitHubOwner, gitHubRepo } = props;
const awsAccountId = Stack.of(this).account;
const gitHubActionsOidcRole = new aws_iam.Role(
this,
'GitHubActionsOidcRole',
{
assumedBy: new aws_iam.FederatedPrincipal(
`arn:aws:iam::${awsAccountId}:oidc-provider/token.actions.githubusercontent.com`,
{
StringEquals: {
'token.actions.githubusercontent.com:aud': 'sts.amazonaws.com',
},
StringLike: {
'token.actions.githubusercontent.com:sub': `repo:${gitHubOwner}/${gitHubRepo}:*`,
},
},
'sts:AssumeRoleWithWebIdentity'
),
}
);
// その他 デプロイに必要な権限の定義
}
}
GitHub Actions で次のように aws-actions/configure-aws-credentials を使用して Assume Role を行うワークフローを作成しました。
- name: Assume Role
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: 'ap-northeast-1'
role-to-assume: ${{ env.AWS_OIDC_ROLE_ARN }}
前述の CDK リソースを作成して、上記のワークフローを実行すると次のようなエラーが発生します。
could not assume role with OIDC: No OpenIDConnect provider found in your account for https://token.actions.githubusercontent.com/ABCDE
解決
次のように OIDC プロバイダーおよび IAM ロールでの URL の指定を、エラーで示されていた URL に変更することにより解決しました。今回は既存の URL の末尾に /ABCDE
(環境によって
異なります)を追加する対応となりました。
- OIDC プロバイダー
import { aws_iam } from 'aws-cdk-lib';
import { Construct } from 'constructs';
// OIDC プロバイダー
export class GitHubActionsOidcProviderConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
new aws_iam.OpenIdConnectProvider(this, 'Default', {
url: 'https://token.actions.githubusercontent.com/ABCDE', // 変更
clientIds: ['sts.amazonaws.com'],
});
}
}
- IAM ロール
import { aws_iam, Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
interface GitHubActionsOidcConstructProps {
gitHubOwner: string;
gitHubRepo: string;
}
// GitHub Actions が Assume Role で使用する IAM ロール
export class GitHubActionsOidcConstruct extends Construct {
constructor(
scope: Construct,
id: string,
props: GitHubActionsOidcConstructProps
) {
super(scope, id);
const { gitHubOwner, gitHubRepo } = props;
const awsAccountId = Stack.of(this).account;
const gitHubActionsOidcRole = new aws_iam.Role(
this,
'GitHubActionsOidcRole',
{
assumedBy: new aws_iam.FederatedPrincipal(
`arn:aws:iam::${awsAccountId}:oidc-provider/token.actions.githubusercontent.com/ABCDE`, // 変更
{
StringEquals: {
'token.actions.githubusercontent.com/ABCDE:aud': 'sts.amazonaws.com', // 変更
},
StringLike: {
'token.actions.githubusercontent.com/ABCDE:sub': `repo:${gitHubOwner}/${gitHubRepo}:*`, // 変更
},
},
'sts:AssumeRoleWithWebIdentity'
),
}
);
// その他 デプロイに必要な権限の定義
}
}
おわりに
今回の事象はドキュメントや Issue 等にも情報がなかったのですが、発生環境は GitHub Enterprise だったため、OIDC のエンドポイントが通常とは異なっていたようです。かなりレア事象ではあると思いますが、どなたかの参考になれば幸いです。
以上