在CodeBuild中使用SAM部署或Cloudformation部署

时间:2020-01-10 05:18:22

标签: 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-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

我的template.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}"


现在,如果我运行该项目,这将永远有效。但是每次我需要将输出文件packaged.yaml复制到S3,然后再从S3复制到cloudformation可以使用的某个路径时,因为在cloudformation包之后,输出被保存到诸如C:\ codebuild \ tmp \ output \ src169630847 \ src \ github.com \ naoleyrashmi \ dgweyy \ packaged.yaml,如果我直接使用aws cloudformation部署--template-file packaged.yaml --capabilities CAPABILITY_IAM --stack-name testcvdg,则cloudformation无法获取package.yaml。有没有使用此方法的明确方法?我什至尝试通过使用sam build和sam deploy来使用SAM,但是即使在这里,sam deploy也会要求我确认变更集,并且没有任何办法可以抑制该变更集或将变更集设置为Y。有人可以帮忙吗?

0 个答案:

没有答案