Anyone getting - `Error: Do not directly set the e...
# help
b
Anyone getting -
Error: Do not directly set the environment for a stack?
I’ve initialized empty project, added a stack, used AWS_PROFILE and --stage and I’m getting this somehow 🙂 I’ve added later empty environment in both BaseStack and service stack but didnt help
f
hmm may I see your
lib/index.js
?
b
Of course!
Copy code
import * as sst from '@serverless-stack/resources';
import ShopifyStack from './ShopifyStack';
import BaseStack from './BaseStack';

export default function main(app: <http://sst.App|sst.App>): void {
  // Set default runtime for all functions
  app.setDefaultFunctionProps({
    environment: {},
    runtime: 'nodejs12.x',
  });

  const { httpApi } = new BaseStack(app, 'base');

  new ShopifyStack(app, 'shopify-stack', httpApi);
}
Shopify dummy stack
Copy code
import { HttpApi } from '@aws-cdk/aws-apigatewayv2';
import * as sst from '@serverless-stack/resources';

interface ShopifyStackProps extends sst.StackProps {
  httpApiId: string;
}

const SRC_PATH = 'services/shopify';

export default class ShopifyStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, props: ShopifyStackProps) {
    super(scope, id, props);
    const { httpApiId } = props;

    new sst.Api(this, 'ShopifyApi', {
      defaultFunctionProps: {
        environment: {},
      },
      httpApi: HttpApi.fromHttpApiAttributes(this, 'Api', {
        httpApiId,
      }),
      routes: {
        'GET /': `${SRC_PATH}/src/lambda.handler`,
      },
    });
  }
}
f
environment for a stack
Environment for a stack refers to the AWS account Id + AWS region pair that defines where the stack will be deployed to. It’s a CDK concept that SST tries to automatically configure and not expose to the end user.
It’s actually not related to a Function’s
environment
variables.
Can you change:
Copy code
new ShopifyStack(app, 'shopify-stack', httpApi);
to:
Copy code
new ShopifyStack(app, 'shopify-stack', { httpApi });
b
lol, good catch! not sure where my brain was doing that 🙂 typescript passed since httpApi has httpApiId 😭 thanks @Frank!
f
The error is cryptic! Just submitted a PR to throw better error messages in this case - https://github.com/serverless-stack/serverless-stack/pull/571