|
1 | | -## AWS Serverless Codepipeline Serverlessrepo Publish  |
| 1 | +## AWS CodePipeline SAR Auto-Publish  |
2 | 2 |
|
3 | | -This is a serverless app that publishes applications to AWS Serverless Application Repository. This app creates a Lambda function that a user could then use as an Invoke action target in their CodePipeline. |
| 3 | +This is a serverless app that provides automated publishing of serverless applications to the AWS Serverless Application Repository (SAR) via AWS CodePipeline. See [this tutorial](https://docs.aws.amazon.com/codepipeline/latest/userguide/tutorials-serverlessrepo-auto-publish.html) for a step-by-step walkthrough. |
4 | 4 |
|
5 | 5 | ## Architecture |
6 | 6 |
|
7 | | - |
8 | | - |
9 | | -1. App has a single Lambda function ServerlessRepoPublish lambda. |
10 | | -1. ServerlessRepoPublish lambda is invoked by CodePipeline as part of the Invoke Action of a pipeline. |
11 | | -1. ServerlessRepoPublish lambda is passed the S3 URL of the packaged SAM template in the CodePipeline S3 bucket. |
12 | | -1. ServerlessRepoPublish lambda downloads the template and parses its Metadata to get application information for calls to CreateApplication/UpdateApplication. |
13 | | -1. ServerlessRepoPublish lambda then does the create or update job processor logic: |
14 | | - 1. Call [AcknowledgeJob](https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_AcknowledgeJob.html) API to claim the job. |
15 | | - 1. Read SAM template and parse application metadata. |
16 | | - 1. Call [CreateApplication](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/applications.html) API with metadata and pass SAM template with semantic version from template metadata. |
17 | | - 1. If success, call [PutJobSuccessResult](https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_PutJobSuccessResult.html) |
18 | | - 1. If application already exists |
19 | | - 1. Call [GetApplication](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/applications-applicationid.html) - Application ARN can be parsed from the 4xx error message. NOTE: This isn't the cleanest solution, but it doesn't require an API change to SAR. |
20 | | - 1. Call [UpdateApplication](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/applications-applicationid.html) if any metadata has changed |
21 | | - 1. Call [CreateApplicationVersion](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/applications-applicationid-versions-semanticversion.html) with SAM template. If it already exists, do nothing. |
22 | | - 1. If API calls fail for any other reason, call [PutJobFailureResult](https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_PutJobFailureResult.html) with failure details. |
| 7 | + |
| 8 | + |
| 9 | +This app contains a single Lambda function: ServerlessRepoPublish. It uses convenience helpers from the [serverlessrepo](https://pypi.org/project/serverlessrepo/) python module to publish applications to SAR. |
| 10 | + |
| 11 | +1. A code change is made to a serverless application and pushed to the source repository, which is the source provider of the CodePipeline pipeline. |
| 12 | +2. The code change flows through the pipeline and outputs a packaged SAM template as a stage output. |
| 13 | +3. ServerlessRepoPublish lambda is invoked by CodePipeline as part of the Invoke Action of the pipeline. |
| 14 | +4. ServerlessRepoPublish lambda gets the packaged SAM template from CodePipeline artifact store S3 bucket. |
| 15 | +5. ServerlessRepoPublish lambda calls serverlessrepo.publish_application() with the packaged template as input. It will perform either create or update logic for the serverless application. See [here](https://pypi.org/project/serverlessrepo/) for details on the python module behavior. |
| 16 | +6. ServerlessRepoPublish lambda calls CodePipeline [PutJobSuccessResult](https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_PutJobSuccessResult.html) API with job id if publish is successful. Otherwise, call CodePipeline [PutJobFailureResult](https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_PutJobFailureResult.html) API with job id and failure details from serverlessrepo.publish_application() |
| 17 | + |
| 18 | +## Installation Instructions |
| 19 | + |
| 20 | +For a step-by-step walkthrough of using this app with AWS CodePipeline, see [this tutorial](https://docs.aws.amazon.com/codepipeline/latest/userguide/tutorials-serverlessrepo-auto-publish.html). |
| 21 | + |
| 22 | +You can also embed this app in the same SAM template that defines your CodePipeline and artifact store bucket using [nested apps](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessapplication). Below is a SAM template snippet that nests AWS CodePipeline SAR Auto-Publish app and creates a three-stage (Source, Build, Deploy) pipeline: |
| 23 | + |
| 24 | +```yaml |
| 25 | +AWSTemplateFormatVersion: '2010-09-09' |
| 26 | +Transform: 'AWS::Serverless-2016-10-31' |
| 27 | + |
| 28 | +Resources: |
| 29 | + CodePipelineServerlessRepoPublishApp: |
| 30 | + Type: 'AWS::Serverless::Application' |
| 31 | + Properties: |
| 32 | + Location: |
| 33 | + ApplicationId: 'arn:aws:serverlessrepo:us-east-1:077246666028:applications/aws-serverless-codepipeline-serverlessrepo-publish' |
| 34 | + SemanticVersion: 1.0.0 |
| 35 | + |
| 36 | + Pipeline: |
| 37 | + Type: AWS::CodePipeline::Pipeline |
| 38 | + Properties: |
| 39 | + ArtifactStore: |
| 40 | + Type: S3 |
| 41 | + Location: |
| 42 | + Ref: ArtifactStoreBucket |
| 43 | + RoleArn: !GetAtt PipelineRole.Arn |
| 44 | + Stages: |
| 45 | + - Name: Source |
| 46 | + Actions: |
| 47 | + - Name: Source |
| 48 | + ActionTypeId: |
| 49 | + Category: Source |
| 50 | + Owner: AWS |
| 51 | + Provider: S3 |
| 52 | + Version: '1' |
| 53 | + Configuration: |
| 54 | + S3Bucket: <YourSourceBucket> |
| 55 | + S3ObjectKey: <YourSourceKey> |
| 56 | + OutputArtifacts: |
| 57 | + - Name: SourceArtifact |
| 58 | + RunOrder: '1' |
| 59 | + - Name: Build |
| 60 | + Actions: |
| 61 | + - Name: Build |
| 62 | + ActionTypeId: |
| 63 | + Category: Build |
| 64 | + Owner: AWS |
| 65 | + Provider: CodeBuild |
| 66 | + Version: '1' |
| 67 | + Configuration: |
| 68 | + ProjectName: <YourCodeBuildProjectName> |
| 69 | + InputArtifacts: |
| 70 | + - Name: SourceArtifact |
| 71 | + OutputArtifacts: |
| 72 | + - Name: BuildArtifact |
| 73 | + RunOrder: '1' |
| 74 | + - Name: Deploy |
| 75 | + Actions: |
| 76 | + - Name: DeployToServerlessRepo |
| 77 | + ActionTypeId: |
| 78 | + Category: Invoke |
| 79 | + Owner: AWS |
| 80 | + Provider: Lambda |
| 81 | + Version: '1' |
| 82 | + Configuration: |
| 83 | + FunctionName: !GetAtt CodePipelineServerlessRepoPublishApp.Outputs.ServerlessRepoPublishFunctionName # Here we use the app output ServerlessRepoPublishFunctionName |
| 84 | + InputArtifacts: |
| 85 | + - Name: BuildArtifact |
| 86 | + RunOrder: '1' |
| 87 | + |
| 88 | + PipelineRole: |
| 89 | + Type: AWS::IAM::Role |
| 90 | + Properties: |
| 91 | + AssumeRolePolicyDocument: |
| 92 | + Statement: |
| 93 | + - Action: ['sts:AssumeRole'] |
| 94 | + Effect: Allow |
| 95 | + Principal: |
| 96 | + Service: [codepipeline.amazonaws.com] |
| 97 | + Version: '2012-10-17' |
| 98 | + Path: / |
| 99 | + Policies: |
| 100 | + - PolicyName: CodePipelineAccess |
| 101 | + PolicyDocument: |
| 102 | + Version: '2012-10-17' |
| 103 | + Statement: |
| 104 | + - Action: |
| 105 | + - 'iam:PassRole' |
| 106 | + Effect: Allow |
| 107 | + Resource: '*' |
| 108 | + - Effect: Allow |
| 109 | + Action: |
| 110 | + - "codebuild:BatchGetBuilds" |
| 111 | + - "codebuild:StartBuild" |
| 112 | + Resource: |
| 113 | + - <YourCodeBuildProjectArn> |
| 114 | + - Effect: Allow |
| 115 | + Action: |
| 116 | + - "lambda:InvokeFunction" |
| 117 | + Resource: |
| 118 | + - !GetAtt CodePipelineServerlessRepoPublishApp.Outputs.ServerlessRepoPublishFunctionArn # Here we use the app output ServerlessRepoPublishFunctionArn |
| 119 | + - Action: |
| 120 | + - 's3:ListBucket' |
| 121 | + - 's3:GetBucketVersioning' |
| 122 | + Effect: Allow |
| 123 | + Resource: |
| 124 | + - !Sub ${ArtifactStoreBucket.Arn} |
| 125 | + - <YourSourceBucketArn> |
| 126 | + - Action: |
| 127 | + - 's3:PutObject' |
| 128 | + - 's3:GetObject' |
| 129 | + - 's3:GetObjectVersion' |
| 130 | + Effect: Allow |
| 131 | + Resource: |
| 132 | + - !Sub ${ArtifactStoreBucket.Arn}/* |
| 133 | + - <YourSourceBucketArn> |
| 134 | + |
| 135 | + ArtifactStoreBucket: |
| 136 | + Type: AWS::S3::Bucket |
| 137 | + Properties: |
| 138 | + VersioningConfiguration: |
| 139 | + Status: Enabled |
| 140 | +``` |
| 141 | +
|
| 142 | +## App Parameters |
| 143 | +
|
| 144 | +1. `LogLevel` (optional) - Log level for Lambda function logging, e.g., ERROR, INFO, DEBUG, etc. Default: INFO |
| 145 | + |
| 146 | +## App Outputs |
| 147 | + |
| 148 | +1. `ServerlessRepoPublishFunctionName` - ServerlessRepoPublish lambda function name. |
| 149 | +1. `ServerlessRepoPublishFunctionArn` - ServerlessRepoPublish lambda function ARN. |
23 | 150 |
|
24 | 151 | ## License Summary |
25 | 152 |
|
26 | | -This sample code is made available under the MIT license. |
| 153 | +This code is made available under the MIT license. See the LICENSE file. |
0 commit comments