1. Answers
  2. Delay Resource Creation in Infrastructure as Code

How do I delay resource creation using Infrastructure as Code?

To delay resource creation or control the sequence in which resources are created, we typically simulate dependencies between the resources. For instance, if we have an AWS S3 bucket that should only be created after an EC2 instance is ready, we can use a dummy null resource to introduce a delay.

Let’s explore this concept with an example:

  1. Create an AWS EC2 instance.
  2. Use a null resource with a depends_on attribute to delay the S3 bucket creation until after the EC2 instance has been created.

This example demonstrates setting up an EC2 instance and then using a null resource to control the timing of the S3 bucket creation.

import * as pulumi from "@pulumi/pulumi";
import * as _null from "@pulumi/null";
import * as aws from "@pulumi/aws";
import * as command from "@pulumi/command";

// Define an EC2 instance resource
const example = new aws.ec2.Instance("example", {
    ami: "ami-0c55b159cbfafe1f0",
    instanceType: aws.ec2.InstanceType.T2_Micro,
    tags: {
        Name: "DelayedInstance",
    },
});
// Define a null resource with a dependency on the EC2 instance
const delay = new _null.Resource("delay", {}, {
    dependsOn: [example],
});
const delayProvisioner0 = new command.local.Command("delayProvisioner0", {create: "echo 'Delay completed, you can now create the S3 bucket!'"}, {
    dependsOn: [delay],
});
// Define an S3 bucket that depends on the null resource
const bucket = new aws.s3.BucketV2("bucket", {bucket: "my-delayed-bucket-12345"}, {
    dependsOn: [delay],
});
export const ec2InstanceId = example.id;
export const s3BucketId = bucket.id;

In this example, we utilized a null resource to introduce a dummy dependency that ensures the S3 bucket creation occurs only after the EC2 instance has been created. The depends_on attribute is key here, as it creates an explicit dependency chain.

To summarize, controlling the sequence of resource creation can be effectively managed using dummy resources and dependencies, ensuring precise control over the workflow of your infrastructure setup.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up