简体   繁体   中英

How to access self variable array type?

I have defined su.netIds in serverless:

enviroment:
    subnetIds:
        - subnet1
        - subnet2

Now, at function define enviroment, I want to get each su.net like that:

environment:
       SUBNET_ID_1: ${self:enviroment.subnetIds[0]}
       SUBNET_ID_2: ${self:enviroment.subnetIds[1]}

I am looking for a solution that can do it.

once you have the env variables set in the provider "environment" you do not need to assign it to each function.

provider:
    enviroment:
        subnet1
        subnet2

and use it as

process.env[subnet1] 
process.env[subnet2]

There is no way you can direct self refer from provider -> environment. But you can do it this way.

provider:
  environment: 
    subnetIds:
      - ${self:custom.subnet1}
      - ${self:custom.subnet2}

functions:
  hello:
    environment:
      SUBNET_ID_1: ${self:custom.subnet1}
      SUBNET_ID_2: ${self:custom.subnet2}

custom:
  subnet1: "subnet1 value"
  subnet2: "subnet2 value"

Another thing also worth to highlight is the provider -> environment will be available in all functions by default. So you need to think again do you really need to re-declare again?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM