What you’ll learn
  • how to modify cloud infrastructure resources deployed by Webiny

Overview
anchor

Users use the webiny deploy command to deploy their Webiny project, into an environment of their choice. During the deployment, apart from building the actual project applications, the command also ensures that a set of required cloud infrastructure resources is deployed.

To deploy necessary cloud infrastructure resources, by default, Webiny relies on Pulumi, a modern infrastructure as code framework. Find out more in the following IaC with Pulumi key topic.

Read more about the cloud infrastructure resources that get deployed into your AWS account in our Cloud Infrastructure key topics section.

And although the cloud infrastructure resources that Webiny deploys are already configured in the best possible manner, there are still cases where some modifications might be needed. In some cases even, the deployed cloud infrastructure needs to be expanded by introducing additional resources into the mix.

To do this, we rely on four webiny.application.ts configuration files, located in each of the four project application folders in every Webiny project:

  1. Core (apps/core/webiny.application.tsexternal link)
  2. API (apps/api/webiny.application.tsexternal link)
  3. Admin (apps/admin/webiny.application.tsexternal link)
  4. Website (apps/website/webiny.application.tsexternal link)

Which webiny.application.ts config file needs to be changed depends on the nature of the change that needs to be made.

For example, let’s imagine we want to adjust the configuration of the Amazon S3 bucket that’s deployed for storing files uploaded via Webiny File Manager. Since this bucket is deployed as part of the Core project application, naturally, we’d want to apply changes via the apps/core/webiny.application.tsexternal link configuration file.

The following example demonstrates how the bucket can be adjusted to enable object versioningexternal link, which essentially enables keeping multiple variants of an object in the same bucket:

apps/core/webiny.application.ts

As we can see, using the pulumi parameter, we’ve passed a relatively simple callback function that enables object versioning in two steps:

  1. references the relevant bucket via the fileManagerBucket const
  2. uses the reference to enable bucket versioning, via the fileManagerBucket.config.versioning

Once the above code change has been made, all that is left to do is a redeploy by running the following command:

With the deployment successfully completed, the object versioning should be enabled for your project (for all files that are uploaded via Webiny File Manager).

When redeploying, make sure to redeploy the project application within which the changes were actually made. In the above example, we’ve made changes within the Core application’s webiny.application.ts file, meaning the Core application needs to be redeployed in order to actually see the changes.

Examples
anchor

In this section, we cover a couple of additional examples of modifying cloud infrastructure resources that get deployed with every Webiny project.

Tagging Cloud Infrastructure Resources
anchor

The following example shows how to apply tags to all cloud infrastructure resources deployed as part of the Core project application. Note that the same approach can be used for all four applications.

apps/core/webiny.application.ts

Note that the tagResources function applies tags only to resources that can actually be tagged. Full list of resources can be found hereexternal link.

Pulumi Resource Name Prefixes
anchor

By default, all cloud infrastructure resources deployed by Webiny are prefixed with the wby- prefix. If needed, this prefix can be changed via the pulumiResourceNamePrefix parameter, via the four webiny.application.ts configuration files, located in each of the four project application folders in every Webiny project:

  1. Core (apps/core/webiny.application.tsexternal link)
  2. API (apps/api/webiny.application.tsexternal link)
  3. Admin (apps/admin/webiny.application.tsexternal link)
  4. Website (apps/website/webiny.application.tsexternal link)

For example:

apps/core/webiny.application.ts

Note that, in the above example, we’ve adjusted the prefix within the Core application’s webiny.application.tsexternal link configuration file, but the same works with other applications as well.

Furthermore, note that the prefix can also be assigned dynamically, via a callback function. The following example shows how we can dynamically assign the prefix based on the environment name.

apps/core/webiny.application.ts

Retrieving the Deployment Environment
anchor

Upon running the webiny deploy command, we specify the environment into which we want to deploy our project (or a specific project application). In more complex cases, we may need to retrieve the environment in order to conditionally apply different configuration to our cloud infrastructure.

The following example shows how we can read the environment into which the application is being deployed.

apps/core/webiny.application.ts

Note that, in the above example, we’ve made the changes within the Core application’s webiny.application.tsexternal link configuration file, but the same works with other applications as well.

Furthermore, this approach can also be used with other parameters, not just pulumi. For example, we can use the same approach when defining the resource name prefix.

apps/core/webiny.application.ts

Defining Multiple Production Environments
anchor

Upon running the webiny deploy command, when prod is passed as the environment name, a Webiny project is deployed in the so-called production deployment modeexternal link.

On order to use the production deployment mode for environments other than prod, we can use the productionEnvironments parameter. The following example shows how we can use the production mode for the staging environment:

apps/core/webiny.application.ts

Note that when specifying additional production environments, they should be specified across all project applications, meaning:

  1. Core (apps/core/webiny.application.tsexternal link)
  2. API (apps/api/webiny.application.tsexternal link)
  3. Admin (apps/admin/webiny.application.tsexternal link)
  4. Website (apps/website/webiny.application.tsexternal link)

Increasing Memory Size for AWS Lambda Functions
anchor

Increasing the memory size for AWS Lambda functions is a common practice when dealing with performance issues. The following example shows how we can increase the memory size for the graphql function, which is the function responsible for serving Webiny’s GraphQL API:

apps/api/webiny.application.ts

Modifying AWS IAM Roles
anchor

The following example demonstrates how we can modify the default AWS IAM role that’s assigned to the AWS Lambda function that represents your GraphQL API (the one accessed via the https://xyz.cloudfront.net/graphql URL).

To learn more about the default GraphQL API and differences between it and the Headless CMS GraphQL API, please check out the Introduction section of the Extend GraphQL API article.

If you want to learn more about the main GraphQL API and how it works on the cloud infrastructure level, check out the GraphQL Requests page of the Cloud Infrastructure - API key topics section.

Amazon OpenSearch (ElasticSearch) Service
anchor

Adjusting Configuration
anchor

Amazon OpenSearch Service is deployed as part of the Core project application. In order to make changes to it, we need to make changes in the apps/core/webiny.application.ts configuration file.

Note that, depending on the version of Webiny you’re using, you may be using either Amazon OpenSearch Service or Amazon ElasticSearch Service. The following examples cover both cases.

Amazon OpenSearch Serviceexternal link was introduced with Webiny version 5.39.0. If you’re using an older version of Webiny, you’re using Amazon ElasticSearch Service.

Amazon OpenSearch Service

The following example shows how we can adjust the configuration of the deployed domain when using Amazon OpenSearch Service (for projects created with Webiny version 5.39.0 or greater).

apps/core/webiny.application.ts
Amazon ElasticSearch Service

The following example shows how we can adjust the configuration of the deployed domain when using Amazon ElasticSearch Service (for projects created before the 5.39.0 release).

apps/core/webiny.application.ts

Using a Shared Amazon OpenSearch Service Domain
anchor

For development purposes, sometimes it’s reasonable to set up a single Amazon OpenSearch domain that will be shared across multiple environments your Webiny project is deployed into. This can be achieved via the openSearch parameter, via the two webiny.application.ts configuration files:

  1. apps/core/webiny.application.tsexternal link
  2. apps/api/webiny.application.tsexternal link
apps/core/webiny.application.ts
apps/api/webiny.application.ts

Note that the configuration must be applied within both webiny.application.ts files. Once applied, in order for the changes to take effect, redeploying the Core and API applications is required. As usual, this can be done via the webiny deploy command:

Furthermore, note that, upon passing a shared Amazon OpenSearch Service domain, via the indexPrefix property, we can define the prefix that will be used with names of all OpenSearch indexesexternal link for the given environment. This is important because it allows us to have a single Amazon OpenSearch Service domain, but also have separate indexes for each environment. Without this, all environments would share the same indexes, which is not what we want because it would cause data from one environment to be visible in another.

Finally, note that the sharing of Amazon OpenSearch Service domains is only recommended for development purposes. For production, it’s recommended to have a separate Amazon OpenSearch Service domain for each environment.

Cross-AWS Account Amazon OpenSearch Service Domain Sharing

The above example demonstrates how to share an Amazon OpenSearch Service domain across multiple environments within the same AWS account. However, in some cases, we may need to share an Amazon OpenSearch Service domain across multiple AWS accounts.

Unfortunately, at this time, we weren’t able to provide full instructions on how to achieve this. If this is something you need, please reach out via our Community Slackexternal link channel, and we’ll be happy to help you out.

Other
anchor

Creating a Cron Job With AWS Lambda and Amazon CloudWatch
anchor

In this example, we introduce a new AWS Lambda function which is triggered once every minute.

Note that the code assumes the AWS Lambda function’s code is located in the apps/api/myCronJob folder. For the full code, please check our webiny-examplesexternal link GitHub repository.

apps/api/webiny.application.ts

Adjusting Amazon CloudFront Distribution Configuration
anchor

A single Webiny project deploys four Amazon CloudFront distributions: one for API, one for Admin, and two for Website project application.

Therefore, if there’s a need to apply a change across all distributions, the change needs to be applied via the following three webiny.application.ts configuration files:

  1. apps/api/webiny.application.tsexternal link
  2. apps/admin/webiny.application.tsexternal link
  3. apps/website/webiny.application.tsexternal link

The following example demonstrates how we can adjust the TLS version for the distribution that’s deployed as part of the API project application.

apps/api/webiny.application.ts

Following the above example, say we want to add additional and serve specific static files from our website application, this can often be the case if you need to host files like a sitemap, domain validation files and similar. Adding those files to website/public folder will get them deployed to your S3 that’s serving the website app, but the default CloudFront config won’t allow you to access them. To fix this, you need to add a new configuration to your CloudFront distribution. The below example shows how to add a rule that allows you to serve any file uploaded inside website/public/_static folder. These files would then be accessible by visiting https://your-domain.com/_static/your-file.txt.

apps/website/webiny.application.ts