我正在使用代碼管道來部署我的基礎架構,我希望能夠在不同的環境(dev,staging,prod,...)中部署它。如何處理CodePipeline中的多個環境?
我現在有一些含有「PIP安裝」的說明和「AWS cloudformation包」命令buildspec.yml文件。我還創建了2個管道,一個用於生產,另一個用於開發,指向github上的2個不同分支。我遇到的問題是,因爲在兩個分支中文件都包含相似的資源,所以我在S3存儲桶上存在名稱衝突。
在使用AWS CLI和cloudformation創建或更新一個堆棧,你可以通過使用--parameters選項參數。我想在我創建的2個管道中做類似的事情。
什麼是解決這一問題的最佳解決方案?
的最終目標是自動化我們的基礎設施的部署。我們的基礎架構由用戶,KMS密鑰,Lamdbas(在Python中),組和存儲桶組成。
我已創建的教程以下兩條管線:http://docs.aws.amazon.com/lambda/latest/dg/automating-deployment.html
第一管道被連接到包含代碼,第二個到臨時分支回購的主分支。我的目標是使用第二個管道環境中的第一個管道和臨時分支在臨時環境中自動部署生產環境中的主分支。
我buildspec.yml文件的樣子:
version: 0.1
phases:
install:
commands:
- pip install requests -t .
- pip install simplejson -t .
- pip install Image -t .
- aws cloudformation package --template-file image_processing_sam.yml --s3-bucket package-bucket --output-template-file new_image_processing_sam.yml
artifacts:
type: zip
files:
- new_image_processing_sam.yml
的image_processing_sam.yml文件的樣子:
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: Create a thumbnail for an image uploaded to S3
Resources:
ThumbnailFunction:
Type: "AWS::Serverless::Function"
Properties:
Role: !GetAtt LambdaExecutionRole.Arn
Handler: create_thumbnail.handler
Runtime: python2.7
Timeout: 30
Description: "A function computing the thumbnail for an image."
LambdaSecretEncryptionKey:
Type: "AWS::KMS::Key"
Properties:
Description: "A key used to encrypt secrets used in the Lambda functions"
Enabled: True
EnableKeyRotation: False
KeyPolicy:
Version: "2012-10-17"
Id: "lambda-secret-encryption-key"
Statement:
-
Sid: "Allow administration of the key"
Effect: "Allow"
Principal:
AWS: "arn:aws:iam::xxxxxxxxxxxxx:role/cloudformation-lambda-execution-role"
Action:
- "kms:Create*"
- "kms:Describe*"
- "kms:Enable*"
- "kms:List*"
- "kms:Put*"
- "kms:Update*"
- "kms:Revoke*"
- "kms:Disable*"
- "kms:Get*"
- "kms:Delete*"
- "kms:ScheduleKeyDeletion"
- "kms:CancelKeyDeletion"
Resource: "*"
-
Sid: "Allow use of the key"
Effect: "Allow"
Principal:
AWS:
- !GetAtt LambdaExecutionRole.Arn
Action:
- "kms:Encrypt"
- "kms:Decrypt"
- "kms:ReEncrypt*"
- "kms:GenerateDataKey*"
- "kms:DescribeKey"
Resource: "*"
LambdaExecutionRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: "LambdaExecutionRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
-
PolicyName: LambdaKMS
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "kms:Decrypt"
Resource: "*"
-
Effect: "Allow"
Action:
- "lambda:InvokeFunction"
Resource: "*"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
UserGroup:
Type: "AWS::IAM::Group"
LambdaTriggerUser:
Type: "AWS::IAM::User"
Properties:
UserName: "LambdaTriggerUser"
LambdaTriggerUserKeys:
Type: "AWS::IAM::AccessKey"
Properties:
UserName:
Ref: LambdaTriggerUser
Users:
Type: "AWS::IAM::UserToGroupAddition"
Properties:
GroupName:
Ref: UserGroup
Users:
- Ref: LambdaTriggerUser
Policies:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: UserPolicy
PolicyDocument:
Statement:
-
Effect: "Allow"
Action:
- "lambda:InvokeFunction"
Resource:
- !GetAtt DispatcherFunction.Arn
Groups:
- Ref: UserGroup
PackageBucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: "package-bucket"
VersioningConfiguration:
Status: "Enabled"
Outputs:
LambdaTriggerUserAccessKey:
Value:
Ref: "LambdaTriggerUserKeys"
Description: "AWSAccessKeyId of LambdaTriggerUser"
LambdaTriggerUserSecretKey:
Value: !GetAtt LambdaTriggerUserKeys.SecretAccessKey
Description: "AWSSecretKey of LambdaTriggerUser"
我已在兩條流水線部署動作來執行計算的變更集在測試行動期間。
第一管線工程就像一個魅力和做的一切我期望它做的事。每次我在主分支中推送代碼時,都會部署它。
我現在面臨的問題是,當我在臨時分支推代碼,一切都在流水線工作,直到達到部署行動。部署操作嘗試創建一個新的堆棧,但由於它與處理的buildspec.yml和image_processing_sam.yml完全相同,因此我會遇到下面的名稱衝突。
package-bucket already exists in stack arn:aws:cloudformation:eu-west-1:xxxxxxxxxxxx:stack/master/xxxxxx-xxxx-xxx-xxxx
LambdaTriggerUser already exists in stack arn:aws:cloudformation:eu-west-1:xxxxxxxxxxxx:stack/master/xxxxxx-xxxx-xxx-xxxx
LambdaExecutionRole already exists in stack arn:aws:cloudformation:eu-west-1:xxxxxxxxxxxx:stack/master/xxxxxx-xxxx-xxx-xxxx
...
有沒有辦法參數化buildspec.yml能夠添加一個後綴在image_processing_sam.yml資源的名字嗎?任何其他想法實現這一點是值得歡迎的。
此致敬禮。
您好,我沒有這個時間這個星期來測試,但我一定要嘗試一下,儘快接受你的答案。感謝您的時間和詳細的答案。 – JonathanGailliez
昨天測試完整代碼示例可在此處獲得第271行: https://github.com/byu-oit-appdev/iac/blob/master/cloudformation/codepipeline/lambda-pipeline-cf.yaml –
嗨,Eric,我終於設法花時間嘗試和驗證您的解決方案。這確實是繼續下去的方式。再次感謝您的詳細解答和您的時間。最好的祝福。 – JonathanGailliez