使用CDK和Python创建instanceprofile

2024-10-02 04:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是cdk新手,尝试用cdk+Python创建一个实例概要文件,代码如下。我已经通过CDK成功创建了角色(gitLabRunner glue),并希望将其与intance配置文件一起使用。但是,当我运行下面的代码时,我得到一个错误gitLabRunner-glue already exists

有人能解释一下我遗漏了什么吗

from aws_cdk import core as cdk
from aws_cdk import aws_glue as glue
from aws_cdk import aws_ec2 as _ec2
from aws_cdk import aws_iam as _iam

class Ec2InstanceProfile(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # role = _iam.Role(self, "instanceprofilerole", role_name="gitLabRunner-glue",
        #              assumed_by=_iam.ServicePrincipal(service='ec2.amazonaws.com'))


        ec2gitLabRunnerinstanceprofile = _iam.CfnInstanceProfile(
            self,
            "ec2gitLabRunnerinstanceprofile",
            instance_profile_name="ec2-gitLabRunner-glue",
            roles=["gitLabRunner-glue"] # also tried with this[role.role_name]
    )

Tags: 代码namefromimportselfawsinitas
2条回答

我让它像这样工作,虽然不是期望的模型,但考虑到cdk的局限性,我找不到任何其他方法

from aws_cdk import core as cdk
from aws_cdk import aws_glue as glue
from aws_cdk import aws_ec2 as _ec2
from aws_cdk import aws_iam as _iam

class Ec2InstanceProfile(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        boundary = _iam.ManagedPolicy.from_managed_policy_arn(self, "Boundary",
                                                           "arn:aws:iam::${ACCOUNT_ID}:policy/app-perm-boundary")

        # role = _iam.Role(self, "instanceprofilerole", role_name="gitLabRunner-glue",
        #              assumed_by=_iam.ServicePrincipal(service='ec2.amazonaws.com'))

        ec2_gitlabrunner_glue = _iam.Role(
            self, 'ec2-gitlabrunner-glue',
            role_name='gitLabRunner-glue',
            description='glue service role to be attached to the runner',
            # inline_policies=[write_to_s3_policy],
            assumed_by=_iam.ServicePrincipal('ec2.amazonaws.com'),
            permissions_boundary=boundary
        )

        ec2gitLabRunnerinstanceprofile = _iam.CfnInstanceProfile(
            self,
            "gitLabRunnerinstanceprofile",
            instance_profile_name="gitLabRunner-glue",
            roles=["gitLabRunner-glue"]
    )

您的AWS帐户中是否已包含具有该名称的角色

cdk中的Cfn函数表示尚未完全连接到所有cdk中的构造和服务。因此,他们通常不会做其他人会做的事情——作为实例概要文件的CloudFormation模板,可能只是挂接到现有角色中,这个cfn函数后面的编码可能会继续,并在模板输出中创建一个角色项

如果您执行cdk synth,请查看cdk.out目录,找到cloudformation模板,然后搜索gitLabRunner glue-您可能会发现正在创建一个AWS::IAM::ROLE,指示cloudformation尝试基于cdk创建的模板运行时,它尝试创建新资源,但无法运行

您可以尝试以下几个选项:

  1. 尝试时,再次取消对角色的注释并使用role.role\u name,但将角色命名为其他名称,或者按照CDK的建议,不要包含名称,让它为您命名

  2. 在aws帐户中搜索角色并将其删除

  3. 如果绝对无法删除现有角色或无法使用新名称创建新角色,请使用(基于导入)导入角色

    role=\u iam.role.from\u role\u arn(self,“ImportedGlueRole”,role\u arn=“arn:aws:of:the:role”,将授权添加到\u resources=True)

要小心一点向资源添加权限-如果不是您的角色来处理cdk,那么如果您这样做,cdk可能会做出更改,这可能会在其他地方引起问题-但是如果不是这样,那么您必须在aws控制台(或cli)中更新角色本身,以接受您的资源

相关问题 更多 >

    热门问题