Amazon web services 在代码构建中使用SAM部署或Cloudformation部署
我是CodeBUild的新手,我正在尝试打包和部署lambda函数。我有我的builspec.yaml文件,其内容如下:Amazon web services 在代码构建中使用SAM部署或Cloudformation部署,amazon-web-services,amazon-cloudformation,aws-codebuild,Amazon Web Services,Amazon Cloudformation,Aws Codebuild,我是CodeBUild的新手,我正在尝试打包和部署lambda函数。我有我的builspec.yaml文件,其内容如下: version: 0.2 phases: install: pre_build: commands: - echo "[Pre-Build phase]" build: commands: - aws cloudformation package --template-file template.yaml --s3-b
version: 0.2
phases:
install:
pre_build:
commands:
- echo "[Pre-Build phase]"
build:
commands:
- aws cloudformation package --template-file template.yaml --s3-bucket <bucketname> --output-template-file packaged.yaml
- aws s3 cp ./packaged.yaml s3://testbucketdemo/packaged.yaml
- aws s3 cp s3://testbucketdemo/packaged.yaml C:\codebuild\tmp\output
post_build:
commands:
- aws cloudformation deploy --template-file C:\codebuild\tmp\output\packaged.yaml --stack-name testcvdg
artifacts:
type: zip
files:
- packaged.yaml
现在,如果我运行这个项目,每次都可以这样做。但每次我需要将输出文件packaged.yaml复制到S3,然后再从S3复制到cloudformation可以使用的某个路径,与cloudformation包一样,输出保存到一些路径,如C:\codebuild\tmp\output\src169630847\src\github.com\naoleyrashmi\dgweyy\packaged.yaml,如果我直接使用aws cloudformation部署--模板文件packaged.yaml--能力_IAM--堆栈名testcvdg,则cloudformation不会获得package.yaml。有没有明确的使用方法?。我甚至尝试通过使用SAM构建和SAM部署来使用SAM,但即使在这里,SAM部署也会要求我确认变更集,并且没有办法抑制该变更集或将变更集设置为Y。有人能帮忙吗。我不太清楚你所说的“云编队没有收到包裹。yaml”是什么意思。你能澄清一下吗。你还可以提供你正在尝试部署的template.yaml的内容吗?我想你想要的是引用。如果您在部署时引用
CODEBUILD\u SRC\u DIR
变量,您将能够删除s3复制,因为您知道它每次都在哪里。谢谢Ben,那么您建议我在部署时使用CODEBUILD\u SRC\u DIR吗?你想做点什么吗?aws cloudformation deploy--模板文件$CODEBUILD\u SRC\u DIR\packaged.yaml--能力能力\u IAM--堆栈名teststack或多或少就是我要做的。我自己还没有测试过,但它可能看起来像$CODEBUILD_SRC_DIR\github.com\naoleyrashmi\dgweyy\package.yaml。您可以通过输出要验证的目录的内容来测试代码构建。我尝试了,但不幸的是没有成功。这里我在构建阶段部署,而不是使用代码部署,这可能是问题所在。我会更新这个,以防我得到任何链接。谢谢
AWSTemplateFormatVersion: "2010-09-09"
Transform: 'AWS::Serverless-2016-10-31'
Description: "My API Gateway and Lambda function"
Parameters:
apiGatewayStageName:
Type: "String"
AllowedPattern: "^[a-z0-9]+$"
Default: "dev"
lambdaFunctionName:
Type: "String"
AllowedPattern: "^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$"
Default: "my-function"
Resources:
apiGateway:
Type: "AWS::ApiGateway::RestApi"
Properties:
Name: "cf-DBTableScheduling"
Description: "DBTableScheduling-apigateway by CF"
dbconn:
Type: "AWS::ApiGateway::Resource"
Properties:
RestApiId: !Ref apiGateway
ParentId: !GetAtt
- apiGateway
- RootResourceId
PathPart: dbconn
apiGatewayRootMethod:
Type: "AWS::ApiGateway::Method"
Properties:
AuthorizationType: "NONE"
HttpMethod: "POST"
Integration:
IntegrationHttpMethod: "POST"
Type: AWS
Credentials: "arn:aws:iam::1231544:role/ApiGatewayInvokeLambdaRole"
Uri: !Sub
- "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations"
- lambdaArn: "arn:aws:lambda:us-east-2:123154878:function:Lambda-DBConnMgmtFunction-3T876IRCHJJGHJG"
ResourceId: !Ref "dbconn"
RestApiId: !Ref "apiGateway"
MethodResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Content-Type: true
apiGatewayDeployment:
Type: "AWS::ApiGateway::Deployment"
DependsOn:
- "apiGatewayRootMethod"
Properties:
RestApiId: !Ref "apiGateway"
StageName: !Ref "apiGatewayStageName"
DBConnMgmtFunctionTesting:
Type: AWS::Serverless::Function
Properties:
CodeUri: DBConnMgmtFunction
Handler: com.test.dbconnmgmt.lambda.DBConnMgmtLambda::handleRequest
Runtime: java8
FunctionName: !Ref "lambdaFunctionName"
MemorySize: 1024
Policies: AmazonDynamoDBFullAccess
Environment:
Variables:
REGION: us-east-2
DYNAMODB_NAME: DBConnectionInfo
lambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
# LogGroupName: !Sub "/aws/lambda/${DBConnMgmtFunctionTesting}"
LogGroupName: !Join
- ''
- - /aws/lambda/
- !Ref lambdaFunctionName
RetentionInDays: 90
Outputs:
apiGatewayInvokeURL:
Value: !Sub "https://${apiGateway}.execute-api.${AWS::Region}.amazonaws.com/${apiGatewayStageName}"