AWS CodeCommit
User Guide (API Version 2015-04-13)

Example: Create an AWS CodeCommit Trigger for an AWS Lambda Function

You can create a trigger for an AWS CodeCommit repository so that events in the repository will invoke a Lambda function. In this example, you will create a Lambda function that returns the URL used to clone the repository to an Amazon CloudWatch log.

Create the Lambda Function

You can create an AWS CodeCommit trigger for a Lambda function as part of creating the function itself in the Lambda console. The following steps include a sample Lambda function. The sample is available in two languages: JavaScript and Python. The function returns the URLs used for cloning a repository to a CloudWatch log.

To create a Lambda function using a Lambda blueprint

  1. Sign in to the AWS Management Console and open the AWS Lambda console at

  2. On the Lambda Functions page, choose Create a Lambda function. (If you have not used Lambda before, choose Get Started Now.)

  3. On the Select blueprint page, choose Blank function.

  4. On the Configure triggers page, choose AWS CodeCommit from the drop-down list of services to integrate with Lambda.

                        Creating a repository from the console
    • In Repository name, choose the name of the repository where you want to configure a trigger that will use the Lambda function in response to repository events.

    • In Trigger name, type a name for the trigger (for example, MyLambdaFunctionTrigger).

    • In Events, choose the repository events that will trigger the Lambda function. If you choose All repository events, you cannot choose any other events. If you want to choose a subset of events, clear All repository events, and then choose the events you want from the list. For example, if you want the trigger to run only when a user creates a tag or a branch in the AWS CodeCommit repository, remove All repository events, and then choose Create branch or tag.

    • If you want the trigger to apply to all branches of the repository, in Branches, choose All branches. Otherwise, choose Specific branches. The default branch for the repository will be added by default. You can keep or delete this branch from the list. Choose up to ten branch names from the list of repository branches.

    • In Custom data, optionally provide information you want included in the Lambda function (for example, the name of the IRC channel used by developers to discuss development in the repository). This field is a string. It cannot be used to pass any dynamic parameters.

    Choose Next.

  5. On the Configure function page, in Name, type a name for the function (for example, MyCodeCommitFunction). Optionally, in Description, type a description for the function. If you want to create a sample JavaScript function, in Runtime, choose Node.js. If you want to create a sample Python function, choose Python 2.7.

  6. In Code entry type, choose Edit code inline, and then replace the hello world code with one of the two following samples.

    For Node.js:

    var aws = require('aws-sdk'); var codecommit = new aws.CodeCommit({ apiVersion: '2015-04-13' }); exports.handler = function(event, context) { //Log the updated references from the event var references = event.Records[0] {return reference.ref;}); console.log('References:', references); //Get the repository from the event and show its git clone URL var repository = event.Records[0].eventSourceARN.split(":")[5]; var params = { repositoryName: repository }; codecommit.getRepository(params, function(err, data) { if (err) { console.log(err); var message = "Error getting repository metadata for repository " + repository; console.log(message);; } else { console.log('Clone URL:', data.repositoryMetadata.cloneUrlHttp); context.succeed(data.repositoryMetadata.cloneUrlHttp); } }); };

    For Python:

    import json import boto3 codecommit = boto3.client('codecommit') def lambda_handler(event, context): #Log the updated references from the event references = { reference['ref'] for reference in event['Records'][0]['codecommit']['references'] } print("References: " + str(references)) #Get the repository from the event and show its git clone URL repository = event['Records'][0]['eventSourceARN'].split(':')[5] try: response = codecommit.get_repository(repositoryName=repository) print("Clone URL: " +response['repositoryMetadata']['cloneUrlHttp']) return response['repositoryMetadata']['cloneUrlHttp'] except Exception as e: print(e) print('Error getting repository {}. Make sure it exists and that your repository is in the same region as this function.'.format(repository)) raise e
  7. In Lambda function handler and role, do the following:

    • In Handler, leave the default value as derived from the function (index.handler for the Node.js sample or lambda_function.lambda_handler for the Python sample).

    • In Role, choose Create a custom role from the list. In the IAM console, do the following:

      • In IAM Role, choose lambda_basic_execution.

      • In Policy Name, choose Create a new role policy.

      • Choose Allow to create the role and eturn to the Lambda console. A value of lambda_basic_execution should now be displayed for Role.


        If you choose a different role or a different name for the role, be sure to use it in the steps in this topic.

    Choose Next.

  8. On the Review page, review the settings for the function, and then choose Create function.

View the Trigger for the Lambda Function in the AWS CodeCommit Repository (Console)

After you have created the Lambda function, you can view and test the trigger in AWS CodeCommit. Testing the trigger will run the function in response to the repository events you specify.

To view and test the trigger for the Lambda function

  1. Open the AWS CodeCommit console at

  2. From the list of repositories, choose the repository where you want to view triggers.

  3. In the navigation pane for the repository, choose Settings. In Settings, choose Triggers.

  4. Review the list of triggers for the repository. You should see the trigger you created in the Lambda console. Choose it from the list.

  5. In the Edit trigger pane, choose Test trigger. This option will attempt to invoke the function with sample data about your repository, including the most recent commit ID for the repository. (If no commit history exists, sample values consisting of zeroes will be generated instead.) This will help you confirm you have correctly configured access between AWS CodeCommit and the Lambda function.

  6. Choose Cancel after you see a success message from the test.

  7. To further verify the functionality of the trigger, make and push a commit to the repository where you configured the trigger. You should see a response from the Lambda function on the Monitoring tab for that function in the Lambda console. From the Monitoring tab, choose View logs in CloudWatch. The CloudWatch console will open in a new tab and display events for your function. Select the log stream from the list that corresponds to the time you pushed your commit. You should see event data similar to the following:

    START RequestId: 70afdc9a-EXAMPLE Version: $LATEST 2015-11-10T18:18:28.689Z 70afdc9a-EXAMPLE References: [ 'refs/heads/master' ] 2015-11-10T18:18:29.814Z 70afdc9a-EXAMPLE Clone URL: END RequestId: 70afdc9a-EXAMPLE REPORT RequestId: 70afdc9a-EXAMPLE Duration: 1126.87 ms Billed Duration: 1200 ms Memory Size: 128 MB Max Memory Used: 14 MB