Creating an AWS EC2 instance using Pulumi in the default VPC and subnet

I have been attempting to create an EC2 instance in the default VPC and default subnet using Pulumi TypeScript, but unfortunately, I haven't had any success. In my script, I am trying to retrieve the default VPC ID and assign it as a string. However, I seem to be having trouble achieving this.

export const defaultvpc = new aws.ec2.DefaultVpc("default", {tags: {Name: "Default VPC",}}).id;

Although I am able to obtain the VPC ID in the 'defaultvpc' variable, I am struggling to use it as an argument in the 'getSubnetIds' function.

const subnetIds = aws.ec2.getSubnetIds({vpcId: defaultvpc})

If anyone has insights on how to successfully create an EC2 instance in the default VPC and default subnet, your assistance would be greatly appreciated.

Answer №1

I have finally uncovered the solution and am excited to share it here!

import * as aws from "@pulumi/aws";
import { VpcIpamResourceDiscovery } from "@pulumi/aws/ec2";

const config = new pulumi.Config();

// Fetching the default VPC ID
const defaultVpcId = aws.ec2.getVpc({
    default: true
}).then(vpc => vpc.id);

// Obtaining the default subnet IDs for the default VPC
const defaultSubnetIds = pulumi.all([defaultVpcId]).apply(([vpcId]) =>
    aws.ec2.getSubnetIds({
        vpcId: vpcId
    })
);

// Setting up an EC2 instance in the first default subnet
const instance = new aws.ec2.Instance("my-instance", {
    instanceType: "t2.micro",
    ami: "ami-123456",  // Replace with desired AMI ID
    subnetId: defaultSubnetIds.apply(subnets => subnets.ids[0]),  // Using the first default subnet ID
});

export const instanceId = instance.id;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The type 'Observable<Response | Observable<Response>>' cannot be assigned to the type 'Observable<Response>'

My service features a basic structure: import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import 'rxjs/add/observable/throw'; import 'rxjs/add/operator/catch'; import ' ...

Objects in the array are failing to sort in the expected sequence

I am facing an issue with sorting an array of objects by a date property using the lodash function orderBy. I have tried to sort it in both ascending and descending order. var searchObj = [{id: 1, postDate: '2/24/2016 5:08 PM'}, ...

The value of an Angular rxjs BehaviorSubject can be updated using the value property directly, without calling

While testing my code, I stumbled upon unexpected mutation. Perhaps I am doing something wrong. User constructor( public id: number, public education: Education[] ){} UserStateService private user = a new BehaviorSubject<User>(null); setUser(us ...

What is the method for assigning 'selective-input' to a form field in Angular?

I am using Angular and have a form input field that is meant to be filled with numbers only. Is there a way to prevent any characters other than numbers from being entered into the form? I want the form to behave as if only integer keys on the keyboard ar ...

Creating a dynamic dropdown menu where the available options in one select box change based on the selection made in another

Looking at the Material-UI Stepper code, I have a requirement to create a select element with dynamic options based on the selected value in another select within the same React component. To achieve this, I wrote a switch function: function getGradeConte ...

Understanding TypeScript - Changing immutability and reversing Readonly<T>

Let's consider a scenario where I have the following mutable class: class Foo { constructor(public bar: any) { } } I am able to create instances of this class with readonly properties like this: const foo: Readonly<Foo> = new Foo(123); fo ...

Retrieving source in Angular from an async function output within a specified time limit

I have a quick query :). I'm attempting to retrieve the image src from an async function, but so far, I haven't had much success. This is what I have: <img [src]="getProductImage(articleNumber)"/> and in my TypeScript file: publi ...

Angular: extracting value from forkJoin nested within another observable's pipe

Here is the scenario that needs to be implemented: An API call is made which returns a response containing an array of objects. The objects are then mapped to another array of objects. For each item in this new array, another API call needs to be made. Th ...

Error: Unable to locate script.exe when spawning the Nodejs process

When trying to run an exe in my electron app, I am encountering an error. Even though the path is correct, it still throws an error. Uncaught Error: spawn exe/0c8c86d42f4a8d77842972cdde6eb634.exe ENOENT at Process.ChildProcess._handle.onexit (inter ...

Tips for organizing content within a book chapter and sections within a meticulously structured database

Can you identify the issue with the function below? How would you suggest fixing it? list() { return this.bookRepo.all().then(books => Promise.all(books.map(book => Promise.all([ this.bookRepo.contents(book.id), ...

Utilize a jest mock object and pass it as a method parameter to achieve seamless testing

I am attempting to create a mock object (ColumnApi from ag-grid) using jest and then pass it as a parameter to a function that calls the "getAllColumns" method from ColumnApi. I am not concerned with how the "getAllColumns" method functions, but I want it ...

Issue encountered while iterating over an array using NgFor

I am currently facing an issue while attempting to create a table for viewing certificates in an account using Angular. The error I am encountering is as follows: ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are ...

How can I choose all the items within an Array that fall within a specific DateTime range?

In order to explore the various methods of querying an array table similar to how MySQL works, I have devised this example. Among the 8 queries presented below, the one that is challenging me is Query (6), which involves using Array.find() in a MySQL Date ...

Node.js: Managing and storing data efficiently

In the development of our messaging application in JavaScript using Node.js, we are focused on ensuring durability by storing text messages and media content outside of ejabberd. For text messages, we have opted for DynamoDB as a DB service. However, as we ...

Strange behavior observed when redirecting in an Angular component inside an async function block

My app is receiving FCM messaging notifications successfully, but I'm facing an issue when trying to trigger a router navigation. Specifically, I want to display a page that mimics the standard Android incoming call screen when a call notification is ...

Implementing React custom component with conditional typing

My goal is to enable other developers to set a click handler for a button only if the button's type is set to button. Users can only set the type to either button or submit. I want to restrict developers from setting the onClick property on the comp ...

Chaining Assignments in TypeScript

let a: { m?: string }; let b = a = {}; b.m = ''; // Property 'm' does not exist on type '{}'. let a: { m?: string } = {}; let b = a; b.m = ''; // It's OK Link to TypeScript Playground What occurs ...

Resetting the initial values in Formik while utilizing Yup validation alongside it

Currently, I am working on a React application with Typescript and using Formik along with Yup validation. However, I have encountered an issue with setting values in a Select element. It seems that the value is not changing at all, or it may be resettin ...

What is the way to declare a prop as optional in Svelte using TypeScript?

I am facing an issue in declaring an optional prop in Svelte with TypeScript. The error message I receive is "Declaration or statement expected". Can someone guide me on how to correctly declare the prop? Definition of My Type: export enum MyVariants { ...

What is causing the error TS2339 to be thrown when attempting to access a class property in this TypeScript illustration: "Property 'text' does not exist on type 'T'"?

Could someone provide insight into why attempting to access a class property in this scenario is resulting in the error message error TS2339: Property 'text' does not exist on type 'T'. Here is the TypeScript code: class Base { rea ...