Use CreateProject with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use CreateProject with an AWS SDK or CLI

The following code examples show how to use CreateProject.

CLI
AWS CLI

Example 1: To create an AWS CodeBuild build project

The following create-project example creates a CodeBuild build project using source files from an S3 bucket

aws codebuild create-project \ --name "my-demo-project" \ --source "{\"type\": \"S3\",\"location\": \"codebuild-us-west-2-123456789012-input-bucket/my-source.zip\"}" \ --artifacts {"\"type\": \"S3\",\"location\": \"codebuild-us-west-2-123456789012-output-bucket\""} \ --environment "{\"type\": \"LINUX_CONTAINER\",\"image\": \"aws/codebuild/standard:1.0\",\"computeType\": \"BUILD_GENERAL1_SMALL\"}" \ --service-role "arn:aws:iam::123456789012:role/service-role/my-codebuild-service-role"

Output:

{ "project": { "arn": "arn:aws:codebuild:us-west-2:123456789012:project/my-demo-project", "name": "my-cli-demo-project", "encryptionKey": "arn:aws:kms:us-west-2:123456789012:alias/aws/s3", "serviceRole": "arn:aws:iam::123456789012:role/service-role/my-codebuild-service-role", "lastModified": 1556839783.274, "badge": { "badgeEnabled": false }, "queuedTimeoutInMinutes": 480, "environment": { "image": "aws/codebuild/standard:1.0", "computeType": "BUILD_GENERAL1_SMALL", "type": "LINUX_CONTAINER", "imagePullCredentialsType": "CODEBUILD", "privilegedMode": false, "environmentVariables": [] }, "artifacts": { "location": "codebuild-us-west-2-123456789012-output-bucket", "name": "my-cli-demo-project", "namespaceType": "NONE", "type": "S3", "packaging": "NONE", "encryptionDisabled": false }, "source": { "type": "S3", "location": "codebuild-us-west-2-123456789012-input-bucket/my-source.zip", "insecureSsl": false }, "timeoutInMinutes": 60, "cache": { "type": "NO_CACHE" }, "created": 1556839783.274 } }

Example 2: To create an AWS CodeBuild build project using a JSON input file for the parameters

The following create-project example creates a CodeBuild build project by passing all of the required parameters in a JSON input file. Create the input file template by running the command with only the --generate-cli-skeleton parameter.

aws codebuild create-project --cli-input-json file://create-project.json

The input JSON file create-project.json contains the following content:

{ "name": "codebuild-demo-project", "source": { "type": "S3", "location": "codebuild-region-ID-account-ID-input-bucket/MessageUtil.zip" }, "artifacts": { "type": "S3", "location": "codebuild-region-ID-account-ID-output-bucket" }, "environment": { "type": "LINUX_CONTAINER", "image": "aws/codebuild/standard:1.0", "computeType": "BUILD_GENERAL1_SMALL" }, "serviceRole": "serviceIAMRole" }

Output:

{ "project": { "name": "codebuild-demo-project", "serviceRole": "serviceIAMRole", "tags": [], "artifacts": { "packaging": "NONE", "type": "S3", "location": "codebuild-region-ID-account-ID-output-bucket", "name": "message-util.zip" }, "lastModified": 1472661575.244, "timeoutInMinutes": 60, "created": 1472661575.244, "environment": { "computeType": "BUILD_GENERAL1_SMALL", "image": "aws/codebuild/standard:1.0", "type": "LINUX_CONTAINER", "environmentVariables": [] }, "source": { "type": "S3", "location": "codebuild-region-ID-account-ID-input-bucket/MessageUtil.zip" }, "encryptionKey": "arn:aws:kms:region-ID:account-ID:alias/aws/s3", "arn": "arn:aws:codebuild:region-ID:account-ID:project/codebuild-demo-project" } }

For more information, see Create a Build Project (AWS CLI) in the AWS CodeBuild User Guide.

  • For API details, see CreateProject in AWS CLI Command Reference.

JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Create a project.

import { ArtifactsType, CodeBuildClient, ComputeType, CreateProjectCommand, EnvironmentType, SourceType, } from "@aws-sdk/client-codebuild"; // Create the AWS CodeBuild project. export const createProject = async ( projectName = "MyCodeBuilder", roleArn = "arn:aws:iam::xxxxxxxxxxxx:role/CodeBuildAdmin", buildOutputBucket = "xxxx", githubUrl = "https://...", ) => { const codeBuildClient = new CodeBuildClient({}); const response = await codeBuildClient.send( new CreateProjectCommand({ artifacts: { // The destination of the build artifacts. type: ArtifactsType.S3, location: buildOutputBucket, }, // Information about the build environment. The combination of "computeType" and "type" determines the // requirements for the environment such as CPU, memory, and disk space. environment: { // Build environment compute types. // https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html computeType: ComputeType.BUILD_GENERAL1_SMALL, // Docker image identifier. // See https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html image: "aws/codebuild/standard:7.0", // Build environment type. type: EnvironmentType.LINUX_CONTAINER, }, name: projectName, // A role ARN with permission to create a CodeBuild project, write to the artifact location, and write CloudWatch logs. serviceRole: roleArn, source: { // The type of repository that contains the source code to be built. type: SourceType.GITHUB, // The location of the repository that contains the source code to be built. location: githubUrl, }, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'b428b244-777b-49a6-a48d-5dffedced8e7', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // project: { // arn: 'arn:aws:codebuild:us-east-1:xxxxxxxxxxxx:project/MyCodeBuilder', // artifacts: { // encryptionDisabled: false, // location: 'xxxxxx-xxxxxxx-xxxxxx', // name: 'MyCodeBuilder', // namespaceType: 'NONE', // packaging: 'NONE', // type: 'S3' // }, // badge: { badgeEnabled: false }, // cache: { type: 'NO_CACHE' }, // created: 2023-08-18T14:46:48.979Z, // encryptionKey: 'arn:aws:kms:us-east-1:xxxxxxxxxxxx:alias/aws/s3', // environment: { // computeType: 'BUILD_GENERAL1_SMALL', // environmentVariables: [], // image: 'aws/codebuild/standard:7.0', // imagePullCredentialsType: 'CODEBUILD', // privilegedMode: false, // type: 'LINUX_CONTAINER' // }, // lastModified: 2023-08-18T14:46:48.979Z, // name: 'MyCodeBuilder', // projectVisibility: 'PRIVATE', // queuedTimeoutInMinutes: 480, // serviceRole: 'arn:aws:iam::xxxxxxxxxxxx:role/CodeBuildAdmin', // source: { // insecureSsl: false, // location: 'https://...', // reportBuildStatus: false, // type: 'GITHUB' // }, // timeoutInMinutes: 60 // } // } return response; };