A Walkthrough

AWS CloudFormation Custom Resource Types

custom types header

AWS CloudFormation now allows you to create your own custom resource types with the new Resource Provider Toolkit. This post walks you through the new toolkit's features and how to create your own custom resource type.

How it works

A custom resource type is named using the same format as any other of the official AWS types, provided they do not conflict with any existing names in the top-level namespace, so you can create YourOrg::ResourceGroup::ResourceType and use that type in your templates without any other special requirements. Types are accessible to anyone in your account.

A custom type package is constructed with a JSON schema that defines your type and the code (handlers) that perform the required actions, which are:

  • Create: creates the new resource from scratch.
  • Read: determines the current state of the resource. This will enable other features in the future, like drift detection.
  • Update: updates an existing resource to a new desired state.
  • Delete: deletes an existing resource.
  • List: returns a list of all resources of that type, regardless of whether they were created within CloudFormation.

Logs from execution of your type can also be set up to be delivered to a CloudWatch Logs Group for debugging and QA purposes. Note that because the execution environment contains private details (usually the desired property values), you should take care not to log these details for security purposes.

CloudFormation registry console

cfn registry

With the new toolkit, a new section of the CloudFormation console is also available. The resource types registry shows a listing of all available types. Digging into each type shows its schema, ARN, description and a link to the resource type's documentation.

Setting up your environment

The CloudFormation CLI (cfn), included with the toolkit is a helpful tool that will bootstrap a lot of the development work required with scaffolding code and project structuring. For the initial release, Java is the default language; however, other languages should be available (like Python and Go) shortly after launch. You should install any JDK 8 to start, such as Amazon Corretto.

For Mac users with Homebrew installed, the following can be run to install the CloudFormation CLI:

1
brew update
2
brew install maven python awscli
3
pip install cloudformation-cli
4
pip install cloudformation-cli-java-plugin

For other systems, check out the official docs for information on how to install the CloudFormation CLI on your system.

The CloudFormation CLI

The CloudFormation CLI will create project scaffolding for you that will allow you to jump straight in to development. Because the initialization creates files relative to your current path, you'll probably want to create a new empty directory and open a terminal at that path.

Run the command cfn init which will first prompt you for the type you want to create. The type name should be in the format CompanyName::Group::Thing. You will then be prompted for the Java path for the main package, usually the default value will be fine here.

The directory will now contain everything needed to package a new custom type, with the following files of note:

pom.xml: The Maven project configuration file, used for declaring dependencies

corpname-group-thing.json: The JSON schema file which defines the properties of the type

src/main/java/com/corpname/group/thing/ActionHandler.java: The Java source files which define the behaviour for each of the handlers

src/test/java/com/corpname/group/thing/ActionHandler.java: The Java files which define the unit tests for each of the handlers

Writing your schema

In your working directory, you should now see a .json file that contains an example schema. Here's the schema we'll use and a description of the fields:

1
{
2
"typeName": "MyCorp::EC2::KeyPair",
3
"description": "Provides an EC2 key pair resource. A key pair is used to control login access to EC2 instances. This resource requires an existing user-supplied key pair.",
4
"sourceUrl": "https://github.com/iann0036/cfn-types/blob/master/mycorp-ec2-keypair/README.md",
5
"properties": {
6
"KeyName": {
7
"description": "The name for the key pair.",
8
"type": "string"
9
},
10
"PublicKey": {
11
"description": "The public key material.",
12
"type": "string"
13
},
14
"Fingerprint": {
15
"description": "The MD5 public key fingerprint as specified in section 4 of RFC 4716.",
16
"type": "string"
17
}
18
},
19
"additionalProperties": false,
20
"required": [
21
"KeyName",
22
"PublicKey"
23
],
24
"additionalIdentifiers": [
25
[
26
"/properties/Fingerprint"
27
]
28
],
29
"readOnlyProperties": [
30
"/properties/Fingerprint"
31
],
32
"writeOnlyProperties": [
33
"/properties/PublicKey"
34
],
35
"primaryIdentifier": [
36
"/properties/KeyName"
37
],
38
"handlers": {
39
"create": {
40
"permissions": [
41
"ec2:ImportKeyPair"
42
]
43
},
44
"read": {
45
"permissions": [
46
"ec2:DescribeKeyPairs"
47
]
48
},
49
"update": {
50
"permissions": [
51
"ec2:DeleteKeyPair",
52
"ec2:ImportKeyPair"
53
]
54
},
55
"delete": {
56
"permissions": [
57
"ec2:DeleteKeyPair"
58
]
59
},
60
"list": {
61
"permissions": [
62
"ec2:DescribeKeyPairs"
63
]
64
}
65
}
66
}

typeName: This should match the type name you defined when you ran cfn init.

description: A description of the type and what it does.

sourceUrl: The location of your documentation and source code. This will be available as a link from the CloudFormation Registry section of the console.

properties: The types and other attributes of the properties within the type. This follows the draft-07 JSON schema specification.

additionalProperties: Whether non-explicit properties (properties you haven't defined) are allowed to be passed in.

required: A list of the required property names. In this case, the key name and the public key material are required and creation should be rejected if those properties are not present.

additionalIdentifiers: An array of single-element arrays which each should contain the path string to the property. A path string should take the form /properties/<FirstLevelProperty>/<SecondLevelProperty>. The value of properties specified in this format will be returned when using a !GetAtt intrinsic function.

readOnlyProperties: An array of path strings that represent properties that cannot be explicitly set, only returned after creation. This usually means that these properties are identifiers that are returned from the creation of a resource.

writeOnlyProperties: An array of path strings that represent properties that cannot be returned when retrieving the current state of a resource (for example, during a drift detection operation). This usually means that these properties are secrets used for the resource. Note that these properties will still be available to the creation handler.

primaryIdentifier: An single-element array containing a path string that represent the primary identifier for the resource. The value of this property will be returned when using a !Ref intrinsic function.

handlers: A map of required AWS permissions needed for the handler functions to operate. The CloudFormation service role (or the calling user if not present) will need to have these permissions in order for the handler to execute.

Visual Studio Code linter

If you use Visual Studio Code, there is an extension available that will help you during the creation of your schema by highlighting syntax issues as you type. The extension is available via the marketplace or search for "CloudFormation Resource Provider Schema Linter" in your extensions sidebar.

cfn cli schema vscode

Adding dependencies

You can use Maven to add additional dependencies to your project. In this case, we'll add the AWS SDK to our project so the handlers can make requests.

In the top-level of your working directory, the cfn init action will have created the pom.xml file. Within the file, locate the <dependencies> section and add the following entry:

1
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ec2 -->
2
<dependency>
3
<groupId>com.amazonaws</groupId>
4
<artifactId>aws-java-sdk-ec2</artifactId>
5
<version>1.11.623</version>
6
</dependency>

Once done, run the command mvn install to install the dependency into the project.

Writing the handlers

Navigate through your /src/main/... directory until you see your *Handler.java files. Each one of these files contain a class for the Create, Read, Update, Delete and List actions respectively. Here's how you would write your create handler to import the key pair:

Create Handler (CreateHandler.java)

All handlers will stub out a valid function that does nothing. Here's what that looks like:

1
package com.mycorp.ec2.keypair;
3
import com.amazonaws.cloudformation.proxy.AmazonWebServicesClientProxy;
4
import com.amazonaws.cloudformation.proxy.Logger;
5
import com.amazonaws.cloudformation.proxy.ProgressEvent;
6
import com.amazonaws.cloudformation.proxy.OperationStatus;
7
import com.amazonaws.cloudformation.proxy.ResourceHandlerRequest;
9
public class CreateHandler extends BaseHandler<CallbackContext> {
11
@Override
12
public ProgressEvent<ResourceModel, CallbackContext> handleRequest(
13
final AmazonWebServicesClientProxy proxy,
14
final ResourceHandlerRequest<ResourceModel> request,
15
final CallbackContext callbackContext,
16
final Logger logger) {
18
final ResourceModel model = request.getDesiredResourceState();
20
// TODO : put your code here
22
return ProgressEvent.<ResourceModel, CallbackContext>builder()
23
.resourceModel(model)
24
.status(OperationStatus.SUCCESS)
25
.build();
26
}
27
}

The handlers are passed a proxy argument which can be used to send SDK calls into the account which invoked the type, a request argument which contains the information relating to the request including the properties of the type, a callbackContext context and a logger which you can use to send logs to your CloudWatch Logs Group if you have that set up. Let's import some required classes for our ImportKeyPair request:

1
import com.amazonaws.services.ec2.model.ImportKeyPairRequest;
2
import com.amazonaws.services.ec2.model.ImportKeyPairResult;
3
import com.amazonaws.services.ec2.AmazonEC2;
4
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
5
import com.amazonaws.services.ec2.model.KeyPair;

Now, from the TODO comment block we'll start implementation. First we create a standard client (assuming the current region by default):

1
AmazonEC2 client = AmazonEC2ClientBuilder.standard().build();

Then we'll construct the ImportKeyPairRequest with the desired properties from our model:

1
ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest()
2
.withKeyName(model.getKeyName())
3
.withPublicKeyMaterial(model.getPublicKey());

We'll use the proxy to invoke the request in the users account:

1
ImportKeyPairResult result = proxy.injectCredentialsAndInvoke(importKeyPairRequest, client::importKeyPair);

And now that the call has been executed, we'll retrieve the Fingerprint property from the response and update our model. This is one of the properties that can be retrieved with a !GetAtt intrinsic function:

1
model.setFingerprint(result.getKeyFingerprint());

Finally, we'll add the ProgressEvent with the updated model. We'll also wrap the function in a generic try-catch block to handle exceptions correctly. Note that being more explicit with your exception handling (catching specific exception types) will improve the quality of your type.

1
try {
2
AmazonEC2 client = AmazonEC2ClientBuilder.standard().build();
3
4
...
6
return ProgressEvent.<ResourceModel, CallbackContext>builder()
7
.resourceModel(model)
8
.status(OperationStatus.SUCCESS)
9
.build();
10
} catch (Exception e) {
11
logger.log(e.getMessage());
12
}
14
return ProgressEvent.<ResourceModel, CallbackContext>builder()
15
.resourceModel(model)
16
.status(OperationStatus.FAILED)
17
.message("An internal error occurred")
18
.build();

That's the create handler completed. At this point the function could be submitted to the registry and work as intended, however you should also write handler code for the other handlers. To save time, here's a link to code for the rest of the handlers.

Testing

Testing is important to ensure your new type works the way you expect it to. You can write integration tests with AWS SAM (covered here) but I wanted to cover the unit tests briefly.

The tests are performed with the Mockito test framework which allows you to mock responses that the AWS SDK would normally return. We'll start by adding a couple of extra imports that will be needed:

1
import software.amazon.awssdk.awscore.AwsResponse;
2
import software.amazon.awssdk.awscore.AwsResponseMetadata;
3
import software.amazon.awssdk.awscore.AwsResponse.Builder;
4
import software.amazon.awssdk.http.SdkHttpResponse;
5
import static org.mockito.Mockito.when;
6
import static org.mockito.Mockito.any;
7
import java.util.List;
8
import java.util.Map;
9
import com.amazonaws.services.ec2.model.ImportKeyPairRequest;
10
import com.amazonaws.services.ec2.model.ImportKeyPairResult;

Now we'll replace the handleRequest_SimpleSuccess test function with the following:

1
@Test
2
public void handleRequest_SimpleSuccess() {
3
when(proxy.injectCredentialsAndInvoke(any(ImportKeyPairRequest.class), any())).thenReturn(
4
new ImportKeyPairResult()
5
.withKeyName("mykey")
6
.withKeyFingerprint("88:e3:48:8d:3d:61:a3:5c:0b:a3:f8:41:ee:d2:5d:22")
7
);
9
final CreateHandler handler = new CreateHandler();
11
final ResourceModel model = ResourceModel
12
.builder()
13
.keyName("mykey")
14
.publicKey("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDU9sjjIO7wdRJT495agnFi++KlKjHKEBpCnHYHJB4U9+1tZPvSkrRI0nD8LULYIgFVi+VbBxlzLol376kAYK1iCE68B5p1DGRV1ecoJgmJSiBGjEz5BDS1ineLToJtLh2Ccb2xQF1Pe8dcyP8Ogu+/AAG5QMyPK+taXcU20ZlprQ7z8UlRsOdTHiYWFJH8NXkGCihLTBaYNDEz6wmgHTsN5HWkaNz32mH/AEJJfiN7rAmKmbk5DKtJccjP9Wp4mnvXWg22EFowIafksLbxc+P+xQgZN+9Coz+HSo/ePFHiLB7YZoEusMBgAY/UttJbNPoEhSitNuDkcHAUAz9ElWMl")
15
.build();
17
final ResourceHandlerRequest<ResourceModel> request = ResourceHandlerRequest.<ResourceModel>builder()
18
.desiredResourceState(model)
19
.build();
21
final ProgressEvent<ResourceModel, CallbackContext> response
22
= handler.handleRequest(proxy, request, null, logger);
24
assertThat(response).isNotNull();
25
assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS);
26
assertThat(response.getCallbackContext()).isNull();
27
assertThat(response.getCallbackDelaySeconds()).isEqualTo(0);
28
assertThat(response.getResourceModel()).isEqualTo(request.getDesiredResourceState());
29
assertThat(response.getResourceModels()).isNull();
30
assertThat(response.getMessage()).isNull();
31
assertThat(response.getErrorCode()).isNull();
32
}

We're starting by adding the Mockito response handler, that will return the "mykey" key and fingerprint whenever the ImportKeyPair call is made within the test. This is a simple example and some tests may have far more complicated test conditions.

The resource model is also explicitly set with values that replicate a typical model input. The request is performed and the response is retrieved and matched against a set of assertions that ensure the stack is in a valid state (you should also test non-valid states).

That's all that's needed for the create handler test. Once again, we'll save you time with this link to code for the rest of the handler tests.

Submission

Now that the type is complete, we can submit it to the CloudFormation Registry. To do so, first ensure your type compiles correctly by running:

1
mvn package

If no errors occur, we can execute the submission with the following command:

1
cfn submit -v

Note that types are regional so if you wish to use a region that is not your default AWS CLI region, you can instead specify the region explicitly:

1
cfn submit -v --region us-east-1

If you wish to have your type available in all regions, you should submit the type into all regions. After submission, you should receive a message like:

1
Registration in progress with token: 3c27b9e6-dca4-4892-ba4e-3c0example

Because submission is an asynchronous action, we should poll to check the status with the following command:

1
aws cloudformation describe-type-registration --registration-token 3c27b9e6-dca4-4892-ba4e-3c0example

You can keep polling this until you receive the completed status which looks something like this:

1
{
2
"ProgressStatus": "COMPLETE",
3
"Description": "Resource deployment is in ResourceDeploymentFinishingStage, currently in status COMPLETE. \n",
4
"TypeArn": "arn:aws:cloudformation:us-east-1:123456789012:type/resource/MyCorp-EC2-KeyPair",
5
"TypeVersionArn": "arn:aws:cloudformation:us-east-1:123456789012:type/resource/MyCorp-EC2-KeyPair/00000001"
6
}

If this is your first time submitting this type, no further action is needed and you're done. If you are updating an existing type (which uses the same commands), your types default should be updated to reflect the new version. You can do this with the following command:

1
aws cloudformation set-type-default-version --arn arn:aws:cloudformation:us-east-1:123456789012:type/resource/MyCorp-EC2-KeyPair/00000002

You can now use your new custom type in your CloudFormation stacks within your account. Try it out with your sample template.

Summary

The resource provider toolkit allows you to create custom CloudFormation resource types that operate much in the same way traditional AWS resource types do today. So, it provides a way to leverage CloudFormation features such as rollback and changesets for both AWS and non-AWS resources created with the toolkit.

More resources

Here are some further resources which will help you dive deeper into this new functionality: