This message was deleted.
# typescript
s
This message was deleted.
l
What is the difference between build time and run time here?
b
I’m trying to do this:
Copy code
const dynamodb = new DynamoDBClient({
  region: DYNAMODB_REGION || '',
});
but I’m getting this error:
Copy code
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
Is this AWS region? You can get it from your Pulumi config:
Copy code
const awsConfig = new pulumi.Config("aws");
const region = awsConfig.require("region");
b
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
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:
Copy code
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
I’m confused… so right now, I have this configuration… I don’t get how to get my code
Copy 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
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
Right now I’m doing this in the codebase to attempt to hook the configurations from Pulumi into my service as regular environment variables.
Copy code
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
Do you have the
myproject:env:
key in your stack file? Is it an array?
b
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:
Copy code
date; pulumi up -s prod; date
l
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
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
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
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.
Copy code
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
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:
Copy code
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
Ah
l
Actually that code is for ECS task definitions. Are lambda env vars set up the same way? I forget...
b
hm good question
l
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
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
Anywhere after they're instantiated. You need to do it inside an apply though.
Copy code
myResource.apply((res) => <http://pulumi.log.info|pulumi.log.info>("My resource:", res));