github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/eventsources/codecommit.md (about) 1 --- 2 date: 2019-01-31 05:44:32 3 title: CodeCommit 4 weight: 10 5 --- 6 7 In this section we'll walkthrough how to trigger your lambda function in response to [CodeCommit Events](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-notify-lambda.html/). 8 9 # Goal 10 11 Assume that we're supposed to write a Lambda function that is triggered in response to any event emitted by a CodeCommit repository. 12 13 ## Getting Started 14 15 Our lambda function is relatively short: 16 17 ```go 18 import ( 19 awsLambdaEvents "github.com/aws/aws-lambda-go/events" 20 ) 21 22 func echoCodeCommit(ctx context.Context, event awsLambdaEvents.CodeCommitEvent) (interface{}, error) { 23 logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*logrus.Entry) 24 logger.WithFields(logrus.Fields{ 25 "Event": event, 26 }).Info("Event received") 27 return &event, nil 28 } 29 ``` 30 31 Our lambda function doesn't need to do much with the repository message other than log and return it. 32 33 ## Sparta Integration 34 35 With `echoCodeCommit()` defined, the next step is to integrate the **go** function with your application. 36 37 Our lambda function only needs logfile write privileges, and since these are enabled by default, we can use an empty `sparta.IAMRoleDefinition` value: 38 39 ```go 40 func appendCloudWatchLogsHandler(api *sparta.API, 41 lambdaFunctions []*sparta.LambdaAWSInfo) []*sparta.LambdaAWSInfo { 42 lambdaFn, _ := sparta.NewAWSLambda(sparta.LambdaName(echoCodeCommit), 43 echoCodeCommit, 44 sparta.IAMRoleDefinition{}) 45 ``` 46 47 The next step is to add a `CodeCommitPermission` value that represents the 48 [notification settings](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-notify.html). 49 50 ```go 51 repositoryName := gocf.String("MyTestRepository") 52 codeCommitPermission := sparta.CodeCommitPermission{ 53 BasePermission: sparta.BasePermission{ 54 SourceArn: repositoryName, 55 }, 56 RepositoryName: repositoryName.String(), 57 Branches: branches, // may be nil 58 Events: events, // may be nil 59 } 60 ``` 61 62 The `sparta.CodeCommitPermission` struct provides fields that proxy the 63 [RepositoryTrigger](https://docs.aws.amazon.com/codecommit/latest/APIReference/API_RepositoryTrigger.html) 64 values. 65 66 ## Add Permission 67 68 With the subscription information configured, the final step is to 69 add the `sparta.CodeCommitPermission` to our `sparta.LambdaAWSInfo` value: 70 71 ```go 72 lambdaFn.Permissions = append(lambdaFn.Permissions, codeCommitPermission) 73 ``` 74 75 The entire function is therefore: 76 77 ```go 78 func appendCodeCommitHandler(api *sparta.API, 79 lambdaFunctions []*sparta.LambdaAWSInfo) []*sparta.LambdaAWSInfo { 80 81 lambdaFn, _ := sparta.NewAWSLambda(sparta.LambdaName(echoCodeCommit), 82 echoCodeCommit, 83 sparta.IAMRoleDefinition{}) 84 85 repositoryName := gocf.String("MyTestRepository") 86 codeCommitPermission := sparta.CodeCommitPermission{ 87 BasePermission: sparta.BasePermission{ 88 SourceArn: repositoryName, 89 }, 90 RepositoryName: repositoryName.String(), 91 } 92 lambdaFn.Permissions = append(lambdaFn.Permissions, codeCommitPermission) 93 return append(lambdaFunctions, lambdaFn) 94 } 95 ``` 96 97 # Wrapping Up 98 99 With the `lambdaFn` fully defined, we can provide it to `sparta.Main()` and 100 deploy our service. The workflow below is shared by all 101 CodeCmmit-triggered lambda functions: 102 103 * Define the lambda function (`echoCodeCommit`). 104 * If needed, create the required [IAMRoleDefinition](https://godoc.org/github.com/mweagle/Sparta*IAMRoleDefinition) with appropriate privileges. 105 * Provide the lambda function & IAMRoleDefinition to `sparta.NewAWSLambda()` 106 * Create a [CodeCommitPermission](https://godoc.org/github.com/mweagle/Sparta#CodeCommitPermission) value. 107 * Define the necessary permission fields. 108 * Append the `CodeCommitPermission` value to the lambda function's `Permissions` slice. 109 * Include the reference in the call to `sparta.Main()`. 110 111 ## Other Resources 112 113 * Consider the [archectype](https://gosparta.io/reference/archetypes/codecommit/) package to encapsulate these steps. 114 * Use the AWS CLI to inspect the configured triggers: 115 116 ```bash 117 $ aws codecommit get-repository-triggers --repository-name=TestCodeCommitRepo 118 119 { 120 "configurationId": "7dd7933a-a26c-4514-9ab8-ad8cc133f874", 121 "triggers": [ 122 { 123 "name": "MyHelloWorldStack-mweagle_main_echoCodeCommit", 124 "destinationArn": "arn:aws:lambda:us-west-2:123412341234:function:MyHelloWorldStack-mweagle_main_echoCodeCommit", 125 "branches": [], 126 "events": [ 127 "all" 128 ] 129 } 130 ] 131 } 132 ``` 133 134 * Use the AWS CLI to test the configured trigger: 135 136 ```bash 137 $ aws codecommit test-repository-triggers --repository-name TestCodeCommitRepo --triggers name=MyHelloWorldStack-mweagle-MyHelloWorldStack-mweagle_main_echoCodeCommit,destinationArn=arn:aws:lambda:us-west-2:123412341234:function:MyHelloWorldStack-mweagle_main_echoCodeCommit,branches=mainline,preprod,events=all 138 139 { 140 "successfulExecutions": [ 141 "MyHelloWorldStack-mweagle-MyHelloWorldStack-mweagle_main_echoCodeCommit" 142 ], 143 "failedExecutions": [] 144 } 145 146 ``` 147