magic-cfn-resources
Builds Lambda-backed custom Cloudformation resources. When you use magic-cfn-resources
's build
method, a Lambda function is created in your stack and used to build a custom resource. Resources that can be built with magic-cfn-resources
are: SnsSubscription
, DynamoDBStreamLabel
, StackOutputs
, and SpotFleet
.
Provided resources in detail:
SNS Subscriptions
This allows you to manage SNS subscriptions as though they are first-class CloudFormation resources.
DynamoDB Stream Labels
This does not actually create any backend resource, but looks up the label for the stream associated with a DynamoDB table.
This resource will use the stream's label as its PhysicalResourceId, so you can then access the label itself in your template via:
{ "Ref": "LogicalNameOfYourCustomResource" }
CloudFormation StackOutputs
Looks up the Outputs for an existing CloudFormation stack.
You can access the values of the stack's outputs with Fn::GetAtt
{ "Fn::GetAtt": ["LogicalNameOfYourCustomResource", "LogicalNameOfStackOutput"] }
SpotFleet
Makes SpotFleet requests.
DefaultVpc
Looks up the default VPC in the region you've launched your stack in, and provides information about the VPC via Fn::GetAtt
.
{ "Fn::GetAtt": ["LogicalNameOfYourCustomResource", "VpcId"] }
You can use Fn::GetAtt
to obtain the following data:
- VpcId: the default VPC's ID
- AvailabilityZones: an array of strings representing the VPC's availability zones
- AvailabilityZoneCount: the number of availability zones
- PublicSubnets: an array of strings representing the VPC's public subnets
- RouteTable: the ID for the first route table in the VPC
S3NotificationTopicConfig
Creates a notification topic configuration on an S3 bucket.
S3Inventory
Creates an InventoryConfiguration for an existing S3 bucket that is not defined in CloudFormation. If you are defining an S3 bucket in CloudFormation, you can use the native CloudFormation support for this configuration.
To create a magical resource in your own CloudFormation template:
In an existing script or in a new script (i.e. sns-subscription.js
):
const magicCfnResources = require('@mapbox/magic-cfn-resources');
module.exports.SnsSubscription = magicCfnResources.SnsSubscription;
Another example: module.exports.SpotFleet = magicCfnResources.SpotFleet;
In the CloudFormation template of your stack:
const magicCfnResources = require('@mapbox/magic-cfn-resources');
Then, pass in the necessary parameters to magicCfnResources.build
. These are the parameters needed for each resource:
SnsSubscription
const SnsSubscription = magicCfnResources.build({
CustomResourceName: 'SnsSubscription',
LogicalName: 'Logical Name',
S3Bucket: 'Bucket Name',
S3Key: 'Key',
Handler: 'sns-subscription.SnsSubscription',
Properties: {
SnsTopicArn: 'Topic Arn',
Protocol: 'Protocol',
Endpoint: 'Endpoint'
}
});
DynamoDBStreamLabel
const DynamoDBStreamLabel = magicCfnResources.build({
CustomResourcenName: 'DynamoDBStreamLabel',
LogicalName: 'Logical Name',
S3Bucket: 'Bucket Name',
S3Key: 'Key',
Handler: 'dynamodb-stream-label.DynamoDBStreamLabel',
Properties: {
TableName: 'Name of Table',
TableRegion: 'Region'
}
});
StackOutputs
const StackOutputs = magicCfnResources.build({
CustomResourceName: 'StackOutputs',
LogicalName: 'Logical Name',
S3Bucket: 'Bucket Name',
S3Key: 'Key',
Handler: 'stack-outputs.StackOutputs',
Properties: {
StackName: 'Name',
StackRegion: 'region'
}
});
SpotFleet
const SpotFleet = magicCfnResources.build({
CustomResourceName: 'SpotFleet',
LogicalName: 'Logical Name',
S3Bucket: 'Bucket Name',
S3Key: 'Key',
Handler: 'spot-fleet.SpotFleet',
Properties: {
SpotFleetRequestConfigData: { },
Region: 'region',
}
});
DefaultVpc
const DefaultVpd = magicCfnResources.build({
CustomResourceName: 'DefaultVpc',
LogicalName: 'Logical Name',
S3Bucket: 'Bucket Name',
S3Key: 'Key',
Handler: 'index.DefaultVpc',
Properties: {}
});
S3NotificationTopicConfig
const S3TopicConfig = magicCfnResources.build({
CustomResourceName: 'S3NotificationTopicConfig',
LogicalName: 'Logical Name',
S3Bucket: 'Bucket Name',
S3Key: 'Key',
Handler: 'index.DefaultVpc',
Properties: {
Id: 'notif-id',
SnsTopicArn: 'topic arn',
Bucket: 'bucket name',
BucketRegion: 'us-east-1',
EventTypes: ['s3:ObjectCreated:*'],
PrefixFilter: 'prefix',
SuffixFilter: '.jpg',
BucketNotificationResources: [ 'bucket Arn' ]
}
});
S3Inventory
const S3InventoryConfig = magic.build({
CustomResourceName: 'S3Inventory',
LogicalName: 'Logical Name',
S3Bucket: 'Bucket Name',
S3Key: 'Key',
Handler: 'index.S3Inventory',
Properties: {
BucketRegion: 'us-east-1',
Bucket: 'my-bucket',
Id: 'my-inventory-id',
InventoryConfiguration: {
Schedule: { Frequency: 'Daily' },
IsEnabled: true,
Destination: {
S3BucketDestination: {
Bucket: cf.getAtt('my-inventory-bucket', 'Arn'),
Format: 'ORC'
}
},
OptionalFields: ['Size', 'LastModifiedDate', 'EncryptionStatus'],
IncludedObjectVersions: 'Current',
Id: 'my-inventory-id'
}
}
});
Optional Condition
A Condition from your template can also be passed into build
.
i.e.:
const SpotFleet = magicCfnResources.build({
CustomResourceName: 'SpotFleet',
LogicalName: 'Logical Name', // a name to refer to the custom resource being built
S3Bucket: 'Bucket Name', // the S3 bucket the code for the handler lives in
S3Key: 'Key', // the S3 key for where the handler lives
Handler: 'spot-fleet.SpotFleet', // references the handler created in the repository
Properties: {
SpotFleetRequestConfigData: { }, // object with SpotFleet configuration specifics
Region: 'region', // region of the SpotFleet i.e.: 'us-east-1'
},
Condition: 'Condition' // the Logical ID of a condition
});
Merge the resources created with build
with the resources already in the stack's template:. i.e.:
const cloudfriend = require('@mapbox/cloudfriend');
const magicCfnResources = require('@mapbox/magic-cfn-resources');
module.exports = cloudfriend.merge(SnsSubscription, <Stack Resources>);
To build new functions
Check out contributing.md for a discussion of the framework this library provides for writing other functions.