Troubleshooting a Pulumi script in Azure using Typescript and encountering difficulties with function writing

My background is in using terraform, but now I am trying out Pulumi/typescript for the first time. In my codebase, I have two files - index.ts and blob.ts.

The create function in blob.ts is responsible for creating a storage account, resource group, blob container, and sending the storage account key to the monitor.

Unfortunately, I'm facing an issue with the last part of the blob.ts file. When I try to export the storageAccountKeys, I encounter an error saying "Modifiers cannot appear here.ts(1184)".

The goal is to keep only functions in the index.ts file, while storing all other code in separate *.ts files.

I would appreciate any guidance on where I might be making a mistake and if there's a different approach to accessing the storage account key for monitoring purposes.

//     blob.ts       //

import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";

export default function CreateStorageContainer() {
    
    // Create an Azure Resource Group
    const resourceGroup = new resources.ResourceGroup("somegroup", {
        location: "westeurope",
        resourceGroupName: "somegroup",
    });

    // Create an Azure resource (Storage Account)
    const storageAccount = new storage.StorageAccount("sgsomestorageaccount", {
        accountName: "sgsomestorageaccount",
        resourceGroupName: resourceGroup.name,
        sku: {
            name: storage.SkuName.Standard_LRS,
        },
        kind: storage.Kind.StorageV2,
    });

    // Create Blob container with name : 
    const blobContainer = new storage.BlobContainer("someblobcontainer", {
        accountName: storageAccount.name,
        containerName: "someblobcontainer",
        resourceGroupName: resourceGroup.name,
    });

    // Export the primary key of the Storage Account
    export const storageAccountKeys = pulumi.all([resourceGroup.name, storageAccount.name]).apply(([resourceGroupName, accountName]) =>
        storage.listStorageAccountKeys({ resourceGroupName, accountName }));
    const primaryStorageKey = storageAccountKeys.keys[0].value;

}
//  index.ts  //
import CreateStorageContainer from "./blob.ts"
CreateStorageContainer();

Please see screenshot

Please see screenshot

Answer №1

In order to retrieve a value from a function, it must be returned instead of exported.

//     blob.ts       //

import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
import { ProximityPlacementGroupType } from "@pulumi/azure-native/compute/v20191201";

export default function CreateStorageContainer() {
  // Setting up an Azure Resource Group

  const resourceGroup = new resources.ResourceGroup("somegroup", {
    location: "westeurope",
    resourceGroupName: "somegroup",
  });

  // Creating an Azure resource (Storage Account)
  const storageAccount = new storage.StorageAccount("sgsomestorageaccount", {
    accountName: "sgsomestorageaccount",
    resourceGroupName: resourceGroup.name,
    sku: {
      name: storage.SkuName.Standard_LRS,
    },
    kind: storage.Kind.StorageV2,
  });

  // Establishing Blob container with specific name:
  const blobContainer = new storage.BlobContainer("someblobcontainer", {
    accountName: storageAccount.name,
    containerName: "someblobcontainer",
    resourceGroupName: resourceGroup.name,
  });

  // Retrieving the primary key of the Storage Account
  const storageAccountKeys = pulumi
    .all([resourceGroup.name, storageAccount.name])
    .apply(([resourceGroupName, accountName]) =>
      storage.listStorageAccountKeys({ resourceGroupName, accountName })
    );
  const primaryStorageKey = storageAccountKeys.keys[0].value;

  return [ storageAccountKeys, primaryStorageKey ] // make sure to provide desired values here

}

To export these values from Pulumi, the action needs to be done in your index.ts

import CreateStorageContainer from "./blob"

export const foo = CreateStorageContainer();

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

Even after ensuring the proper type checking, I am still receiving the error message "Property 'message' does not exist on type 'object'"

I have the following code snippet: try { // api call } catch (error) { if (typeof error === 'object' && error !== null && 'message' in error) { if (typeof error.message === 'string') { if (error.me ...

Failure to Deploy ASP.NET RC1 on Azure Servers

My ASP.NET Core RC1 application has been deployed to Azure using GitHub deployment. Everything was running smoothly until the past 5 days. I managed to deploy the application successfully around 2016-05-16. However, when I made a recent change and tried to ...

What is the significance of requiring a specific string in a Typescript Record when it is combined with a primitive type in a union?

I am facing an issue with the following data type: type ErrorMessages = Record<number | 'default', string>; When I declare a variable like const text: ErrorMessages = {403: 'forbidden'}, Typescript points out that default is miss ...

"What is the most effective way to utilize and integrate the `setValue` function from react-hook-form within a custom react hook

Struggling to pass setValue to a react hook? In my custom react hook, I need to set values in a form using react-hook-form's setValue. Yet, after migrating from v6 to v7, I'm having trouble figuring out the proper typing for this. This is how t ...

Using RxJS switchMap in combination with toArray allows for seamless transformation

I'm encountering an issue with rxjs. I have a function that is supposed to: Take a list of group IDs, such as: of(['1', '2']) Fetch the list of chats for each ID Return a merged list of chats However, when it reaches the toArray ...

Unable to modify the active property of the specified object as it is read-only

Presented here is the interface: export interface ProductCommand extends ProductDetailsCommand { } This is the ProductDetailsCommand interface: export interface ProductDetailsCommand { id: string; active: boolean; archive: boolean; title: ...

Angular: Understanding the intricacies of HTTP calls in ngOnInit versus click events (potentially related to caching mechanisms)

Within my Angular application, I have a basic service configured to communicate with the server. The service has been injected into a component. Interestingly, when I directly call one of its methods inside the ngOnInit() method of the component, everythin ...

Navigating with Angular 4's router and popping up modals

I have an Angular 4 SPA application that utilizes the Angular router. I am looking to create a hyperlink that will open a component in a new dialog using Bootstrap 4. I am able to open modal dialogs from a function already. My question is, how can I achi ...

I am struggling to extract data from the spawned Node.js child process. What am I overlooking?

Trying to utilize a spawned command-line process for lzip in order to expand an lzipped data stream due to the lack of suitable native JavaScript tools. Succeeded in achieving this by working with files and file descriptors, although cumbersome to handle ...

How can Angular 2 effectively keep track of changes in HTTP service subscriptions? Calling the method directly may result in

After making a call to the authentication service method that checks the validity of the username and password, as well as providing an authentication token, I encountered an issue. When attempting to display the value obtained from calling the getAuthData ...

Testing vue-router's useRoute() function in Jest tests on Vue 3

Struggling with creating unit tests using Jest for Vue 3 components that utilize useRoute()? Take a look at the code snippet below: <template> <div :class="{ 'grey-background': !isHomeView }" /> </template> &l ...

ngOnChanges will not be triggered if a property is set directly

I utilized the modal feature from ng-bootstrap library Within my parent component, I utilized modalService to trigger the modal, and data was passed to the modal using componentInstance. In the modal component, I attempted to retrieve the sent data using ...

How can I set a document ID as a variable in Firebase Firestore?

I recently set up a firestore collection and successfully added data (documents) to it. Firestore conveniently generates unique document ids for each entry. Inserting Data this.afs.collection('questions').add({ 'question': message, &a ...

Adjust the component suppliers based on the @input

If I were to implement a material datepicker with a selection strategy, I would refer to this example There are instances where the selection strategy should not be used. The challenge lies in setting the selection strategy conditionally when it is insta ...

Is it possible for an object to be undefined in NextJS Typescript even after using a guard

Hey there, I could really use some help with a React component I'm working on. This is one of my initial projects and I've encountered an issue involving fetching async data. Below is the current state of my solution: import { Component } from &q ...

Tips for differentiating between elements with identical values in an HTML datalist using Angular

My boss is insisting that I use a datalist in our website interface to select an employee, even though there's no way to determine if the user typed in the name or picked from the list. The challenge is that the list must only display full names, but ...

Set the style of the mat-select element

I'm having an issue with my select option in Angular Material. The options look fine, but when I select one, the strong tag disappears. Can anyone help me style only that part? Thank you in advance. <mat-select formControlName="projectId" ...

The module 'AppModule' is importing an unexpected value 'AppAsideModule'. To resolve this issue, make sure to include an @NgModule annotation

Recently, I attempted to upgrade my Angular project from version 13 to version 17. However, during the process, I encountered an error stating "Unexpected value 'AppAsideModule' imported by the module 'AppModule'. Please add an @NgModul ...

Monitor a universal function category

Trying to implement a TypeScript function that takes a single-argument function and returns a modified version of it with the argument wrapped in an object. However, struggling to keep track of the original function's generics: // ts v4.5.5 t ...

What is the best way to set up TSLint to apply specific rules with one line and different rules with another line

There is a unique method in which I can specify the code to format, such as forcing the else statement to be on the same line as the ending brace of an if statement. "one-line": [ true, "check-open-brace", "check-catch", "check-else", "check-fin ...