Skip to content
v2.276.0

Cache Class ​

Cache options for CodeBuild Project.

A cache can store reusable pieces of your build environment and use them across multiple builds.

Example ​

ts
declare const vpc: ec2.Vpc;
declare const mySecurityGroup: ec2.SecurityGroup;
new pipelines.CodeBuildStep('Synth', {
  // ...standard ShellStep props...
  commands: [
    /* ... */
  ],
  env: {
    /* ... */
  },

  // If you are using a CodeBuildStep explicitly, set the 'cdk.out' directory
  // to be the synth step's output.
  primaryOutputDirectory: 'cdk.out',

  // Control the name of the project
  projectName: 'MyProject',

  // Control parts of the BuildSpec other than the regular 'build' and 'install' commands
  partialBuildSpec: codebuild.BuildSpec.fromObject({
    version: '0.2',
    // ...
  }),

  // Control the build environment
  buildEnvironment: {
    computeType: codebuild.ComputeType.LARGE,
    privileged: true,
    dockerServer: {
      computeType: codebuild.DockerServerComputeType.SMALL,
      securityGroups: [mySecurityGroup],
    },
  },
  timeout: Duration.minutes(90),
  fileSystemLocations: [
    codebuild.FileSystemLocation.efs({
      identifier: 'myidentifier2',
      location: 'myclodation.mydnsroot.com:/loc',
      mountPoint: '/media',
      mountOptions: 'opts',
    }),
  ],

  // Control Elastic Network Interface creation
  vpc: vpc,
  subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
  securityGroups: [mySecurityGroup],

  // Control caching
  cache: codebuild.Cache.bucket(new s3.Bucket(this, 'Cache')),

  // Additional policy statements for the execution role
  rolePolicyStatements: [
    new iam.PolicyStatement({
      /* ... */
    }),
  ],
});
python
# vpc: ec2.Vpc
# my_security_group: ec2.SecurityGroup

pipelines.CodeBuildStep("Synth",
    # ...standard ShellStep props...
    commands=[],
    env={},

    # If you are using a CodeBuildStep explicitly, set the 'cdk.out' directory
    # to be the synth step's output.
    primary_output_directory="cdk.out",

    # Control the name of the project
    project_name="MyProject",

    # Control parts of the BuildSpec other than the regular 'build' and 'install' commands
    partial_build_spec=codebuild.BuildSpec.from_object({
        "version": "0.2"
    }),

    # Control the build environment
    build_environment=codebuild.BuildEnvironment(
        compute_type=codebuild.ComputeType.LARGE,
        privileged=True,
        docker_server=codebuild.DockerServerOptions(
            compute_type=codebuild.DockerServerComputeType.SMALL,
            security_groups=[my_security_group]
        )
    ),
    timeout=Duration.minutes(90),
    file_system_locations=[
        codebuild.FileSystemLocation.efs(
            identifier="myidentifier2",
            location="myclodation.mydnsroot.com:/loc",
            mount_point="/media",
            mount_options="opts"
        )
    ],

    # Control Elastic Network Interface creation
    vpc=vpc,
    subnet_selection=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS),
    security_groups=[my_security_group],

    # Control caching
    cache=codebuild.Cache.bucket(s3.Bucket(self, "Cache")),

    # Additional policy statements for the execution role
    role_policy_statements=[
        iam.PolicyStatement()
    ]
)
java
Vpc vpc;
SecurityGroup mySecurityGroup;

CodeBuildStep.Builder.create("Synth")
        // ...standard ShellStep props...
        .commands(List.of())
        .env(Map.of())

        // If you are using a CodeBuildStep explicitly, set the 'cdk.out' directory
        // to be the synth step's output.
        .primaryOutputDirectory("cdk.out")

        // Control the name of the project
        .projectName("MyProject")

        // Control parts of the BuildSpec other than the regular 'build' and 'install' commands
        .partialBuildSpec(BuildSpec.fromObject(Map.of(
                "version", "0.2")))

        // Control the build environment
        .buildEnvironment(BuildEnvironment.builder()
                .computeType(ComputeType.LARGE)
                .privileged(true)
                .dockerServer(DockerServerOptions.builder()
                        .computeType(DockerServerComputeType.SMALL)
                        .securityGroups(List.of(mySecurityGroup))
                        .build())
                .build())
        .timeout(Duration.minutes(90))
        .fileSystemLocations(List.of(FileSystemLocation.efs(EfsFileSystemLocationProps.builder()
                .identifier("myidentifier2")
                .location("myclodation.mydnsroot.com:/loc")
                .mountPoint("/media")
                .mountOptions("opts")
                .build())))

        // Control Elastic Network Interface creation
        .vpc(vpc)
        .subnetSelection(SubnetSelection.builder().subnetType(SubnetType.PRIVATE_WITH_EGRESS).build())
        .securityGroups(List.of(mySecurityGroup))

        // Control caching
        .cache(Cache.bucket(new Bucket(this, "Cache")))

        // Additional policy statements for the execution role
        .rolePolicyStatements(List.of(
            PolicyStatement.Builder.create().build()))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
Vpc vpc;
SecurityGroup mySecurityGroup;

new CodeBuildStep("Synth", new CodeBuildStepProps {
    // ...standard ShellStep props...
    Commands = new [] {  },
    Env = new Dictionary<string, object> { },

    // If you are using a CodeBuildStep explicitly, set the 'cdk.out' directory
    // to be the synth step's output.
    PrimaryOutputDirectory = "cdk.out",

    // Control the name of the project
    ProjectName = "MyProject",

    // Control parts of the BuildSpec other than the regular 'build' and 'install' commands
    PartialBuildSpec = BuildSpec.FromObject(new Dictionary<string, object> {
        { "version", "0.2" }
    }),

    // Control the build environment
    BuildEnvironment = new BuildEnvironment {
        ComputeType = ComputeType.LARGE,
        Privileged = true,
        DockerServer = new DockerServerOptions {
            ComputeType = DockerServerComputeType.SMALL,
            SecurityGroups = new [] { mySecurityGroup }
        }
    },
    Timeout = Duration.Minutes(90),
    FileSystemLocations = new [] { FileSystemLocation.Efs(new EfsFileSystemLocationProps {
        Identifier = "myidentifier2",
        Location = "myclodation.mydnsroot.com:/loc",
        MountPoint = "/media",
        MountOptions = "opts"
    }) },

    // Control Elastic Network Interface creation
    Vpc = vpc,
    SubnetSelection = new SubnetSelection { SubnetType = SubnetType.PRIVATE_WITH_EGRESS },
    SecurityGroups = new [] { mySecurityGroup },

    // Control caching
    Cache = Cache.Bucket(new Bucket(this, "Cache")),

    // Additional policy statements for the execution role
    RolePolicyStatements = new [] {
        new PolicyStatement(new PolicyStatementProps { }) }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var vpc Vpc
var mySecurityGroup SecurityGroup

pipelines.NewCodeBuildStep(jsii.String("Synth"), &CodeBuildStepProps{
	// ...standard ShellStep props...
	Commands: []*string{
	},
	Env: map[string]interface{}{
	},

	// If you are using a CodeBuildStep explicitly, set the 'cdk.out' directory
	// to be the synth step's output.
	PrimaryOutputDirectory: jsii.String("cdk.out"),

	// Control the name of the project
	ProjectName: jsii.String("MyProject"),

	// Control parts of the BuildSpec other than the regular 'build' and 'install' commands
	PartialBuildSpec: codebuild.BuildSpec_FromObject(map[string]interface{}{
		"version": jsii.String("0.2"),
	}),

	// Control the build environment
	BuildEnvironment: &BuildEnvironment{
		ComputeType: codebuild.ComputeType_LARGE,
		Privileged: jsii.Boolean(true),
		DockerServer: &DockerServerOptions{
			ComputeType: codebuild.DockerServerComputeType_SMALL,
			SecurityGroups: []ISecurityGroup{
				mySecurityGroup,
			},
		},
	},
	Timeout: awscdk.Duration_Minutes(jsii.Number(90)),
	FileSystemLocations: []IFileSystemLocation{
		codebuild.FileSystemLocation_Efs(&EfsFileSystemLocationProps{
			Identifier: jsii.String("myidentifier2"),
			Location: jsii.String("myclodation.mydnsroot.com:/loc"),
			MountPoint: jsii.String("/media"),
			MountOptions: jsii.String("opts"),
		}),
	},

	// Control Elastic Network Interface creation
	Vpc: vpc,
	SubnetSelection: &SubnetSelection{
		SubnetType: ec2.SubnetType_PRIVATE_WITH_EGRESS,
	},
	SecurityGroups: []ISecurityGroup{
		mySecurityGroup,
	},

	// Control caching
	Cache: codebuild.Cache_Bucket(s3.NewBucket(this, jsii.String("Cache"))),

	// Additional policy statements for the execution role
	RolePolicyStatements: []PolicyStatement{
		iam.NewPolicyStatement(&PolicyStatementProps{
		}),
	},
})

Initializer ​

ts
new Cache()
python
Cache()
java
new Cache()
csharp
new Cache()
go
NewCache()

Methods ​

bucket(bucket: IBucket, options?: BucketCacheOptions): Cache // chainable@staticmethod def bucket(bucket: IBucket, options: typing.Optional[BucketCacheOptions] = None) -> Cachepublic static Cache bucket(IBucket bucket, BucketCacheOptions options)public static Cache Bucket(IBucket bucket, BucketCacheOptions? options = null)func Bucket(Bucket IBucket, Options BucketCacheOptions) Cachestatic#
Create an S3 caching strategy.
2 parameters

Parameters

  • bucketbucketbucketbucketBucket — the S3 bucket to use for caching.
  • optionsoptionsoptionsoptionsOptions — additional options to pass to the S3 caching.
local(...modes: LocalCacheMode[]): Cache // chainable@staticmethod def local(modes: LocalCacheMode) -> Cachepublic static Cache local(LocalCacheMode modes)public static Cache Local(LocalCacheMode modes)func Local(Modes LocalCacheMode) Cachestatic#
Create a local caching strategy.
1 parameter

Parameters

  • modesmodesmodesmodesModes — the mode(s) to enable for local caching.
none(): Cache // chainable@staticmethod def none() -> Cachepublic static Cache none()public static Cache None()func None() Cachestatic#