繁体   English   中英

如何使用 Jenkins 的 AWS 角色部署 CDK?

[英]How to deploy CDK with AWS role from Jenkins?

嗨,我已经用 jenkins 创建了 CDK 项目。 我想使用角色进行部署。 例如带有角色的 cdk 部署。 例如在 cloudformation 我正在做如下。

cfn_manage deploy-stack \
  --stack-name "$CFN_CDK_STACK" \
  --template-file "cloudformation/templates/cdk.yml" \
  --parameters-file "$PARAMS_FILE" \
  --role-name infra-cfnrole-location-nonprivileged

现在我有如下 CDK 项目。

checkout scm
              withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',credentialsId: "${env.PROJECT_ID}-aws-${CFN_ENVIRONMENT}"]]) {
                abc = docker.build('cdkimage', "--build-arg http_proxy=${env.http_proxy} --build-arg https_proxy=${env.https_proxy} .")
                abc.inside{
                sh 'ls -la'
                sh "bash ./scripts/build.sh"
              }

然后在 build.sh 里面

NONPRIV_ROLE_NAME='infra-cfnrole-location-nonprivileged'
aws sts assume-role --role-arn 'arn:aws:iam::id:role/infra-cfnrole-location-nonprivileged' --role-session-name jenkins --query '[Credentials.AccessKeyId,Credentials.SecretAccessKey,Credentials.SessionToken]' --output text
cdk synth
cdk deploy

这是抛出错误

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::id:user/infra-prjauth-location is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::187628286232:role/infra-cfnrole-location-nonprivileged

对于受信任关系中的角色 cfnrole-location-nonprivileged 我有以下策略

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "cloudformation.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

有人可以帮我部署角色吗? 任何帮助,将不胜感激。 谢谢

我认为问题在于您在 IAM 角色的信任关系中没有特别信任您的 IAM 用户。

假设此角色具有 CDK 部署所需的正确权限(有关更多信息,请参见此处),您需要允许您的 IAM 用户访问该角色,而不是 cloudformation Cloudformation 已经可以访问您的帐户资源。

我认为这个版本的信任关系策略应该可以解决问题:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Resource": "<full ARN of the relevant user>",
      "Action": "sts:AssumeRole"
    }
  ]
}

让我知道它是否有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM