Cloudinary SDK: Environment Variables
Let's look at how to add Cloudinary SDK access to a SAM template. Basically we just need to make the CLOUDINARY_URL accessible as an environment variable.
We'll start by adding the Cloudinary URL as an environment variable. This opens the door to using the Cloudinary SDK from any function. We'll create three sample functions:
  1. 1.
    Call the URL helper to create a URL that can be used to apply request media and/or apply transformations using GET.
  2. 2.
    Call an Admin API function that reports a list of assets containing a supplied tag using GET with query string parameters.
  3. 3.
    Call the Upload API to upload an image supplying it with a remote URL using POST with JSON object input.

Authentication

We won't be demonstrating how to apply authentication to these functions. You can read more about that in CloudFormation documentation. If you are deploying functions that you suspect could be abused, you will want to add authentication to your template.

Environment Variables

We only need to add 1 environment variable to start using the Cloudinary SDK - namely CLOUDINARY_URL. To add this, we'll need to add a Parameters Section to the template.yaml.
Parameters:
CloudinaryUrl:
Type: String
Description: Cloudinary URL
Default: "CLD_URL"
Globals:
Function:
Timeout: 3
Environment:
Variables:
CLOUDINARY_URL: !Ref CloudinaryUrl
This can be placed above the Globals. Notice that we supply a Default value. This will be replaced with a real value in the samconfig.toml file. We also need to add a section for Environment | Variables to the Globals section. We create an internal reference. We will not need to use that reference in code. The environment variable is made available to the function using the CLOUDINARY_URL as a key. The Cloudinary SDK provides a .config function that will pull out all the credentials contained in the CLOUDINARY_URL and make those available to the SDK functions: cloud_name, api_secret, and api_key. We won't need to reference any of these credentials by name in the function code.

Supplying CLOUDINARY_URL for local testing

You can use sam invoke and sam start api for local testing. Supply the CLOUDINARY_URL locally by creating a file that contains the environment variable key/value pairs per function in the template. Remember that you can have multiple functions in the template. Even though the CLOUDINARY_URL parameter is configured as Global, you still need to the list the variables per function.
We'll be adding new functions to the template that calls the cloudinary.url function. Here's what it will look like. Let's say we create a file name config.json which is used by a template with two functions, F1Function and F2Function. The contents will look like this:
{
"F1Function": {
"CLOUDINARY_URL": "cloudinary://0000000000:[email protected]",
},
"F2Function": {
"CLOUDINARY_URL": "cloudinary://1111111111:[email protected]"
}
}
When we want to run the command from the command line we use the following command where -n is an abbreviation for environment variables:
sam local invoke -n config.json
If we want to start a local web server we can supply the environment variables like this:
sam local start-api -n config.json

Copy link
On this page
Authentication
Environment Variables