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

How Do I Delay Resource Creation Using Infrastructure as Code?

Introduction

In Infrastructure as Code (IaC), managing the sequence of resource creation is crucial for ensuring that resources are provisioned in the correct order. This is particularly important when certain resources depend on others being fully operational before they can be created. This guide will demonstrate how to delay resource creation by simulating dependencies, using AWS as an example.

Step-by-Step Explanation

To illustrate this concept, we’ll walk through a scenario where an AWS S3 bucket should only be created after an EC2 instance is ready. We achieve this by using a null resource to introduce a delay.

Steps:

  1. Create an AWS EC2 Instance: Begin by defining an EC2 instance resource using Pulumi.
  2. Introduce a Delay with a Null Resource: Use a null resource with a depends_on attribute to ensure that the S3 bucket creation is delayed until the EC2 instance is fully set up.
  3. Create an S3 Bucket: Define the S3 bucket and set it to depend on the null resource, ensuring it is only created after the delay.

Here is the TypeScript code demonstrating the setup:

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;

Key Points

  • Dependency Management: The depends_on attribute is essential for creating explicit dependency chains between resources.
  • Null Resource Utilization: Using a null resource allows for the introduction of artificial dependencies, ensuring the correct sequence of resource creation.
  • Controlled Resource Creation: This approach provides precise control over the order in which resources are provisioned.

Conclusion

By leveraging dummy resources and dependency attributes, you can effectively control the sequence of resource creation in your infrastructure setup. This ensures that dependent resources are only created once their prerequisites are fully operational, resulting in a more reliable and predictable infrastructure deployment process.

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