简体   繁体   English

使用 Azure Pipelines 部署 AWS CDK 堆栈时出错(在 CDK 中使用 python)

[英]Error deploying AWS CDK stacks with Azure Pipelines (using python in CDK)

app.py example of how my stacks are defined (with some information changed as you can imagine) app.py 如何定义我的堆栈的示例(您可以想象一些信息已更改)

Stack1(app, "Stack1",env=cdk.Environment(account='123456789', region='eu-west-1'))

In my azure pipeline I'm trying to do a cdk deploy在我的天蓝色管道中,我正在尝试进行 cdk 部署

- task: AWSShellScript@1
  inputs:
    awsCredentials: 'Service_connection_name'
    regionName: 'eu-west-1'
    scriptType: 'inline'
    inlineScript: |
    sudo bash -c "cdk deploy '*' -v --ci --require-approval-never"
  displayName: "Deploying CDK stacks"

but getting errors.但出现错误。 I have the service connection to AWS configured, but the first error was我已配置到 AWS 的服务连接,但第一个错误是

[Stack_Name] failed: Error: Need to perform AWS calls for account [Account_number], but no credentials have been configured [Stack_Name] 失败:错误:需要为账户 [Account_number] 执行 AWS 调用,但尚未配置凭证

Stack_Name and Account_Number have been redacted Stack_Name 和 Account_Number 已被编辑

After this error, I decided to add a step to my pipeline and manually create the files .aws/config and .aws/credentials在这个错误之后,我决定在我的管道中添加一个步骤并手动创建文件 .aws/config 和 .aws/credentials

- script: |
    echo "Preparing for CDK"
    echo "Creating directory"
    sudo bash -c "mkdir -p ~/.aws"
    echo "Writing to files"
    sudo bash -c "echo -e '[default]\nregion = $AWS_REGION\noutput = json' > ~/.aws/config"
    sudo bash -c "echo -e '[default]\naws_access_key_id = $AWS_ACCESS_KEY_ID\naws_secret_access_key = $AWS_SECRET_ACCESS_KEY' > ~/.aws/credentials"
  displayName: "Setting up files for CDK"

After this I believed the credentials would be fixed but it still failed.在此之后,我相信凭据会被修复,但它仍然失败。 The verbose option revealed the following error amongst the output:详细选项在输出中显示以下错误:

Setting "CDK_DEFAULT_REGION" environment variable to将“CDK_DEFAULT_REGION”环境变量设置为

So instead of setting the region to "eu-west-1" it is being set to nothing因此,与其将区域设置为“eu-west-1”,不如将其设置为空

I imagine I'm missing something, so please, educate me and help me get this working我想我错过了一些东西,所以请教育我并帮助我完成这项工作

This happens because you're launching separate instances of a shell with sudo bash , and they don't share the credential environment variables that the AWSShellScript task is populating.发生这种情况是因为您使用sudo bash启动单独的 shell 实例,并且它们不共享AWSShellScript任务正在填充的凭证环境变量。

To fix the credentials issue, replace the inline script with just cdk deploy '*' -v --ci --require-approval never要修复凭据问题,只需将内联脚本替换为cdk deploy '*' -v --ci --require-approval never

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

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