我使用 AWS CodeStar 使用“Express.js Aws Lambda Webservice”CodeStar 模板创建了一个新应用程序。这很棒,因为它使用 AWS CodePipeline 为我设置了一个简单的 CI/CD 管道。默认情况下,管道有 3 个步骤从 git repo 获取源代码,运行构建步骤,然后部署到“开发”环境。
我的问题是我无法设置它,以便我的管道有多个环境:dev、staging 和 prod。
我当前的部署步骤有 2 个操作:GenerateChangeSet和ExecuteChangeSet。以下是原始开发环境构建步骤中操作的配置,效果很好:
我在管道末尾创建了一个新的部署阶段以部署到登台,但老实说,我不确定如何更改配置。我想最终我希望能够进入 AWS 控制台的 AWS Lambda 部分并查看三个独立的 lambda 函数:binance-bot-dev、binance-bot-staging、binance-bot-prod。然后我可以将这些中的每一个设置为 cloudwatch 预定事件或使用他们自己的 api 网关 url 公开。
这是我尝试用于新部署阶段的配置:
我真的不确定这个配置是否正确以及我应该改变什么才能以我想要的方式进行部署。
例如,我应该更改“堆栈名称”,还是应该将其保留为“awscodestar-binance-bot-lambda”,或者像我在这里一样为每个环境更改它?
另外,我指向项目中的另一个 template.yml 文件。原始 template.yml 如下所示:
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar
Parameters:
ProjectId:
Type: String
Description: AWS CodeStar projectID used to associate new resources to team members
Resources:
Dev:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs4.3
Environment:
Variables:
NODE_ENV: dev
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /
Method: get
PostEvent:
Type: Api
Properties:
Path: /
Method: post
对于 template.staging.yml,我使用完全相同的配置,只是在“资源”下将“Dev:”更改为“Staging:”,并且还更改了 NODE_ENV 环境变量的值。所以,我基本上想知道这是我想要实现的正确配置吗?
假设配置中的所有内容都是正确的,那么我需要解决此错误。如上所述设置所有内容后,我可以运行我的管道,但是当它进入我的暂存构建步骤时,GenerateChage_Staging操作失败并显示以下错误消息:
操作执行失败用户:arn:aws:sts::954459734159:assumed-role/CodeStarWorker-binance-bot-CodePipeline/1524253307698 无权执行:cloudformation:DescribeStacks on resource:arn:aws:cloudformation:us-east-1 :954459734159:stack/awscodestar-binance-bot-lambda-staging/*(服务:AmazonCloudFormation;状态代码:403;错误代码:AccessDenied;请求 ID:dd801664-44d2-11e8-a2de-8fa6c42cbf86)
从这条错误消息看来,我需要为我的“CodeStarWorker-binance-bot-CodePipeline”添加“cloudformation:DescribeStacks”,所以我转到 IAM -> Roles 并单击 CodeStarWorker-binance-bot-CodePipeline 角色. 但是,当我单击“CodeStarWorker-binance-bot-CodePipeline”并深入了解 CloudFormation 的策略信息时,看起来该角色已经拥有“DescribeStacks”的权限!
如果有人能指出我做错了什么或提供任何指导来理解和思考如何使用 AWS CodePipeline 做多个环境,那就太好了。谢谢!
更新:
我将 Deploy_To_Staging 管道阶段中的“堆栈名称”更改回“awscodestar-binance-bot-lambda”。但是,然后我从 GenerateChange_Staging 操作中收到此错误:
操作执行失败无效的模板路径:binance-bot-BuildArtifact::template-export.staging.yml。工件 binance-bot-BuildArtifact 不存在
更新 2: 在我的项目的根目录中,我有 CodeStar 生成的 buildspec.yml 文件。它看起来像这样:
version: 0.2
phases:
install:
commands:
# Install dependencies needed for running tests
- npm install
# Upgrade AWS CLI to the latest version
- pip install --upgrade awscli
pre_build:
commands:
# Discover and run unit tests in the 'tests' directory
- npm test
build:
commands:
# Use AWS SAM to package the application using AWS CloudFormation
- aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml
- aws cloudformation package --template template.staging.yml --s3-bucket $S3_BUCKET --output-template template-export.staging.yml
- aws cloudformation package --template template.prod.yml --s3-bucket $S3_BUCKET --output-template template-export.prod.yml
artifacts:
type: zip
files:
- template-export.yml
然后我将其添加到 CloudFormation 部分:
然后我将其添加到“构建:-> 命令:”部分:
- aws cloudformation package --template template.staging.yml --s3-bucket $S3_BUCKET --output-template template-export.staging.yml
- aws cloudformation package --template template.prod.yml --s3-bucket $S3_BUCKET --output-template template-export.prod.yml
我将此添加到“文件:”
- 模板-export.staging.yml
- 模板-export.prod.yml
但是,我仍然收到“binance-bot-BuildArtifact 不存在”的错误。
这是进行 buildspec.yml 更改后的完整错误:
操作执行失败无效的模板路径:binance-bot-BuildArtifact::template-export.staging.yml。工件 binance-bot-BuildArtifact 不存在
我可以在管道的一个阶段而不是另一个阶段访问“binance-bot-BuildArtifact”,这对我来说似乎很奇怪。难道构建工件仅在构建阶段之后直接可用于一个管道阶段?有人可以帮我访问这个“binance-bot-BuildArtifact”吗?谢谢!