generated from linz/template-python-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (46 loc) · 1.96 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
CDK application entry point file.
"""
import constructs
from aws_cdk import App, CfnOutput, CfnParameter, Stack, aws_iam
class OidcProviderStack(Stack):
def __init__(self, scope: constructs.Construct, construct_id: str) -> None:
super().__init__(scope, construct_id)
env_name = CfnParameter(self, "EnvName", type="String", description="The environment to deploy the OidcProviderStack")
github_repo = CfnParameter(
self,
"GithubRepo",
type="String",
description="Specify the parameters that limit which GitHub repo has access to AWS",
)
aws_iam.OpenIdConnectProvider(
self,
"GithubAwsOidcProvider",
url="https://token.actions.githubusercontent.com",
client_ids=["sts.amazonaws.com"],
)
account_id = aws_iam.AccountRootPrincipal().account_id
oidc_deploy_role = aws_iam.Role(
self,
"OidcDeployRole",
role_name=f"{env_name.value_as_string}Oidc",
assumed_by=aws_iam.WebIdentityPrincipal(
f"arn:aws:iam::{account_id}:oidc-provider/token.actions.githubusercontent.com",
{
"StringLike": {
"token.actions.githubusercontent.com:aud": ["sts.amazonaws.com"],
"token.actions.githubusercontent.com:sub": [f"repo:{github_repo.value_as_string}"],
}
},
),
)
oidc_deploy_role.add_managed_policy(aws_iam.ManagedPolicy.from_aws_managed_policy_name("AdministratorAccess"))
# Set Cfn to output deploy_role arn post CDK deployment.
# Assign or update this value in AWS_ASSUME_ROLE in GitHub secrets
CfnOutput(self, "ServiceAccountIamRole", value=oidc_deploy_role.role_arn)
def main() -> None:
app = App()
OidcProviderStack(app, "OidcProviderStack")
app.synth()
if __name__ == "__main__":
main()