At Ynpact, we are constantly looking for innovative solutions to simplify and optimize cloud development on AWS. With this in mind, we practice a DevOps culture, making extensive use of automation tools and infrastructure as code (IaC). As experienced users of Open Tofu (Terraform) and the Serverless framework, we noticed a gap: the absence of iterators in the Serverless framework, unlike Open Tofu.

To overcome this insufficiency, we have developed a new plugin for the Serverless framework, introducing two powerful and easy-to-use operators: « repeat » and « foreach ». Inspired by Terraform’s « count » and « foreach » operators, these tools allow you to repeat and iterate YAML blocks dynamically and efficiently.

A plugin to boost your productivity

Plugin overview

Our plugin @ynpact/serverless-plugin-foreach allows you to integrate two new operators into your serverless templates: « repeat » and « foreach ». These operators give you the flexibility to manage repetitive resources without complexity, improving the readability, consistency and maintainability of your configurations.

The “repeat” operator

The « repeat » operator allows you to repeat a YAML block a certain number of times. You can use the variable {{i}} inside this block, which will be replaced by the repeat index.

Example :

plugins:
  - '@ynpact/serverless-plugin-foreach'
custom:
  template:
    bucket{{i}}:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: bucket_{{i}}
resources:
  - ${repeat(${self:custom.template}, 3)}

Result generated:

Resources:
  bucket0:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: bucket_0
  bucket1:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: bucket_1
  bucket2:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: bucket_2

You can also customize the initial index value and add padding to the indices. See Github documentation.

The “foreach” operator

The « foreach » operator allows you to repeat a YAML block for each element of a list of values. Inside this block you can use {{i}} for the index and {{i.<key>}} for specific values ​​of list elements.

Example :

plugins:
  - '@ynpact/serverless-plugin-foreach'
custom:
  template:
    bucket{{i}}:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: "{{i.name}}"
        PublicAccessBlockConfiguration:
          BlockPublicAcls: "{{i.blockPublicAcls}}"
  bucketParams:
    - name: my-public-bucket
      blockPublicAcls: false
    - name: my-private-bucket
      blockPublicAcls: true
resources:
  - ${foreach(${self:custom.template}, ${self:custom.bucketParams})}

Result generated :

Resources:
  bucket0:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-public-bucket
      PublicAccessBlockConfiguration:
        BlockPublicAcls: false
  bucket1:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-private-bucket
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true

Consult the complete documentation on the repository Github or npm you plugin.

Installation and use

Installing our plugin is quick and easy:

npm install @ynpact/serverless-plugin-foreach --save-dev

Then add it to your file serverless.yml :

plugins:
  - '@ynpact/serverless-plugin-foreach'

And use « repeat » and « foreach » operators as needed.

Tips: You can also use the 2 operators in conjunction with the “file” operator to externalize your template and configuration into separate files as follows:

plugins:

  - '@ynpact/serverless-plugin-foreach'

resources:

  - ${foreach(   ${file(template/bucketTemplate.yml.tpl)}, ${file(conf/${opt:stage}.yml):buketsParams}   )}

Consult the complete documentation on the repository Github or npm you plugin

Why choose Ynpact?

At Ynpact, we pride ourselves on offering solutions that make the daily lives of cloud developers easier. Our expertise in AWS allows us to understand the challenges you face and develop adapted tools to overcome them. The “Serverless foreach” plugin is a perfect illustration of this: simple to use, powerful and flexible, it helps you automate and optimize your Serverless deployments.

We invite you to test our plugin and give us your feedback. For any questions or suggestions, do not hesitate to contact us. We are also open to contributions via pull requests on our GitHub repository.

Together, let’s simplify the cloud.

The Ynpact team