https://pulumi.com logo
Title
b

bitter-salesclerk-37287

11/16/2022, 8:43 PM
Hey guys, anybody know how I can access configuration variables at build-time, say, for instantiating a singleton object? I think I’m able to access configuration variables now from my code, at RT, but it’s not obvious to me how to do so for build-time… Usually, it just works and I don’t ever have to think about this distinction, but with Pulumi, it seems more complicated, when it comes to what I normally just call environment variables in programming. I already got some help on the basics, but again I’m stuck on configurations.
l

little-cartoon-10569

11/16/2022, 8:43 PM
What is the difference between build time and run time here?
b

bitter-salesclerk-37287

11/16/2022, 8:45 PM
I’m trying to do this:
const dynamodb = new DynamoDBClient({
  region: DYNAMODB_REGION || '',
});
but I’m getting this error:
Diagnostics:
  pulumi:pulumi:Stack (content-updater-prod):
    error: Running program '/Users/riyad/repos/content-updater' failed with an unhandled exception:
    Error: Region is missing
        at resolveRegionConfig (/Users/riyad/repos/content-updater/node_modules/@dr-squatch/lib/node_modules/@aws-sdk/config-resolver/dist-cjs/regionConfig/resolveRegionConfig.js:9:15)
        at new DynamoDBClient (/Users/riyad/repos/content-updater/node_modules/@dr-squatch/lib/node_modules/@aws-sdk/client-dynamodb/dist-cjs/DynamoDBClient.js:22:69)
        at Object.<anonymous> (/Users/riyad/repos/content-updater/node_modules/@dr-squatch/lib/dist/aws/dynamodb.js:8:20)
        at Module._compile (node:internal/modules/cjs/loader:1101:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
        at Module.require (node:internal/modules/cjs/loader:1005:19)
        at require (node:internal/modules/cjs/helpers:102:18)
        at Object.<anonymous> (/Users/riyad/repos/content-updater/node_modules/@dr-squatch/lib/dist/aws/index.js:4:18)
… maybe I’m confused… maybe it is RT. Just trying to be able to instantiate my dynamodb instance, and possibly other things, using my environment variables / configurations.
ah silly me, so before, I was trying to do it in a helper library, but then I commented out that instantiation thinking maybe it was an anti-pattern to do that (though I think it can be helpful)… because Pulumi just doesn’t seem to send environment variables along to dependent libraries, it seems, from my attempting to get it to work… So then I just wrote that line locally, but I forgot to update/link the latest version of my library in my Pulumi codebase.
l

little-cartoon-10569

11/16/2022, 8:47 PM
Is this AWS region? You can get it from your Pulumi config:
const awsConfig = new pulumi.Config("aws");
const region = awsConfig.require("region");
b

bitter-salesclerk-37287

11/16/2022, 8:48 PM
That is the same region for me for Dynamodb, but it’s possible that I could use a different region for Dynamodb than for AWS in general, isn’t it?
l

little-cartoon-10569

11/16/2022, 8:49 PM
No. If you need to put DynamoDB in a different region, you would need a different provider. You can create two providers in your Pulumi program of course. In which case, the region you want is whichever one you passed to the provider that you created your DynamoDb instance with.
If DynamoDB already exists and isn't managed within Pulumi, then you'll need to pass its region in via config.
In which case, the same solution applies, you'll just use the normal project config instead of the AWS config:
const config = new pulumi.Config();
const region = config.require("dynamoDbRegion");
Re: run time vs. build time: there is no difference in Pulumi. Pulumi normally uses ts-node, so bulid and run time are the same. In you package.json, you can put all your Pulumi program's dependencies under "devDependencies", so in node-world, everything is essentially build-time.
b

bitter-salesclerk-37287

11/16/2022, 9:01 PM
I’m confused… so right now, I have this configuration… I don’t get how to get my code
const dynamodb = new DynamoDBClient({
  region: DYNAMODB_REGION || '',
});
to work (esp from my npm installed dependency @dr-squatch/lib — which is currently private)… I also can’t comment out my dynamodb instantiation in my helper library, as I have other services (via Serverless Framework) successfully consuming that. If I move everything to devDependencies, my eslint complains, though I don’t exactly understand how that might work/help.
l

little-cartoon-10569

11/16/2022, 9:03 PM
Put your DynamoDB region in your Pulumi.stackName.yaml file, as described here: https://www.pulumi.com/docs/intro/concepts/config/ There will be one of these files for each stack (e.g. dev, staging, europe, etc...), and each can have a different value for each config key. Then use the code I posed above to get the configuration value. It will return the value from the file that corresponds to your current stack.
b

bitter-salesclerk-37287

11/16/2022, 9:08 PM
Right now I’m doing this in the codebase to attempt to hook the configurations from Pulumi into my service as regular environment variables.
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
import contentUpdater from './useCases';

const serviceName = 'content-updater';
const config = new pulumi.Config();
const envVars = config.requireObject('env');

const lambda = new aws.lambda.CallbackFunction(`${serviceName}-lambda`, {
  policies: [aws.iam.ManagedPolicies.CloudWatchLogsFullAccess],
  environment: {
    variables: {
      ...envVars as any,
    },
  },
  callback: contentUpdater,
});
I must be missing something simple here? I thought I had this working earlier, but potentially I suppose it’s possible that I may have modified something since then.
l

little-cartoon-10569

11/16/2022, 9:09 PM
Do you have the
myproject:env:
key in your stack file? Is it an array?
b

bitter-salesclerk-37287

11/16/2022, 9:11 PM
I just deleted the other env vars from my file, but my file generally looks like this
And I shared above, the screenshot of what the configs look like in the dashboard in Pulumi, from the web browser.
And this is the command I run to attempt to deploy:
date; pulumi up -s prod; date
l

little-cartoon-10569

11/16/2022, 9:19 PM
I see that env is defined as an object with key:value pairs. Maybe it should be an array? Not sure....
Maybe not, the spread operator should do fine in this situation..
Though
variables: envVars as Record<string, string>
might also work.. What error are you getting right now?
b

bitter-salesclerk-37287

11/16/2022, 11:12 PM
That shouldn’t make a difference, as it’s the types in typescript… Those types are ignored at RT… type-casting in typescript is syntactic sugar at RT, practically speaking. I haven’t looked much further into it since, but tomorrow I might just convert this service to use Serverless Framework if I can’t get this Pulumi configuration stuff hooked up to my environment variables properly.
l

little-cartoon-10569

11/16/2022, 11:46 PM
I know, I just wondered if the object might be a completely different shape to what you're expecting. Can you inspect / introspect it to make sure it's correct? If one thing is expecting an array and the other is an object with multiple key pairs, then it won't work.
b

bitter-salesclerk-37287

11/16/2022, 11:50 PM
hm well I just ran it still doesn’t work, but I just ran it without the { …envVars as any }, as I realized that was just unnecessarily making a new object.
const lambda = new aws.lambda.CallbackFunction(`${serviceName}-lambda`, {
  policies: [aws.iam.ManagedPolicies.CloudWatchLogsFullAccess],
  environment: {
    variables: envVars as any,
  },
  callback: contentUpdater,
});
… I still have the error. What does this screenshot tell us? Is there some other way I’m supposed to be accessing the environment variables? It seems like all the values are there in Pulumi, so I don’t get what happens when I attempt to build the project (which is basically where I was originally today).
l

little-cartoon-10569

11/17/2022, 12:11 AM
I would get
env
then look at it in a debugger / log statements, and compare that to what Function.environment.variables is expecting. FWIW, in our projects that put Pulumi config values into lambda environments, we have this code. Don't know if it's needed, but I inherited this and didn't bother investigating it for efficiency:
const environmentVariables = new Array<KeyValuePair>();
const configEnvironmentVariables = pulumiConfig.getObject(
  "environmentVariables",
) as Record<string, string>;
Object.entries(configEnvironmentVariables).forEach(([k, v]) => {
  environmentVariables.push({ name: k, value: v.toString() });
});
The
environmentVariables
variable is assigned directly to environment.variables.
That is, we're converting an object to an array. So maybe that's what you need.
b

bitter-salesclerk-37287

11/17/2022, 12:12 AM
Ah
l

little-cartoon-10569

11/17/2022, 12:13 AM
Actually that code is for ECS task definitions. Are lambda env vars set up the same way? I forget...
b

bitter-salesclerk-37287

11/17/2022, 12:13 AM
hm good question
l

little-cartoon-10569

11/17/2022, 12:14 AM
Hmm, no, they don't seem to be. They're a simple object, same as the Pulumi config object...
Ah well, it's gotta be something along those lines...
b

bitter-salesclerk-37287

11/17/2022, 12:14 AM
Yeah, probably, based on my IDE docs.
I don’t really know how/where to console.log things during the
pulumi up
run, but I was trying to do that earlier today, though.
l

little-cartoon-10569

11/17/2022, 1:39 AM
Anywhere after they're instantiated. You need to do it inside an apply though.
myResource.apply((res) => <http://pulumi.log.info|pulumi.log.info>("My resource:", res));