Files
floci/compatibility-tests/compat-cdk/lib/floci-stack.ts
T
Mario Toffia a338a430cc feat(cloudformation): provision Cloud Map namespaces and services (#4599)
* feat(cloudformation): provision Cloud Map namespaces and services

AWS::ServiceDiscovery::PrivateDnsNamespace, PublicDnsNamespace,
HttpNamespace and Service were stubbed, so a stack reported them created
while nothing existed in Cloud Map and Fn::GetAtt resolved to literals.

CloudMapCfnProvisioner creates them through CloudMapService. Ref returns
the id; Fn::GetAtt exposes Id and Arn, HostedZoneId on a DNS namespace and
Name on a service. An unnamed service is named <LogicalId>-<12 chars>, as
measured on AWS. Description, DnsConfig, HealthCheckConfig and Tags update
in place; a createOnly change replaces the resource through
ReplacementCleanup, and a rolled-back replacement restores the createOnly
record. A Ref to a Number parameter is coerced back to a JSON number for
DnsRecords TTL and FailureThreshold.

Closes #4596
* fix(cloudformation): roll back in-place Cloud Map updates

An in-place namespace or service update followed by a later resource
failing left the Cloud Map changes live and the stack in
UPDATE_ROLLBACK_FAILED, since rollbackUpdate only undid replacements.
The provisioner now keeps the description, DnsConfig, HealthCheckConfig
and tags an in-place update overwrites and puts them back on rollback;
a committed update drops the snapshot.
* fix(cloudmap): reject DNS record type changes on service update

UpdateService only changes the TTLs of a service's existing DNS records on AWS. An in-place CloudFormation update that changes the record types, or gives a DnsConfig to a service without one, now fails with InvalidInput instead of being applied, so it rolls back like on AWS and a rollback never has to remove a DnsConfig.
* fix(cloudmap): accept only TTL changes in a service DnsConfig update

UpdateService carries only DnsRecords on AWS and CloudFormation does not support a RoutingPolicy update. Comparing the record types as a set let a RoutingPolicy change or an added record of the same type through; the requested and stored DnsConfig now have to match apart from the TTLs.
* fix(cloudmap): ignore DNS record order in a service DnsConfig update

A TTL-only update that listed the same records in another order was rejected. The comparison now uses the sorted record types, so counts, types and RoutingPolicy still have to match.
* test(cloudformation): restore a real HealthCheckConfig in the Cloud Map rollback test

The in-place rollback test went from a null health check config to null, so dropping it from the snapshot would have passed.
2026-09-30 21:45:18 -05:00

119 lines
4.9 KiB
TypeScript

import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as servicediscovery from 'aws-cdk-lib/aws-servicediscovery';
import * as path from 'path';
import { Construct } from 'constructs';
export class FlociTestStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const bucket = new s3.Bucket(this, 'TestBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const queue = new sqs.Queue(this, 'TestQueue', {
queueName: 'floci-cdk-test-queue',
visibilityTimeout: cdk.Duration.seconds(30),
});
const table = new dynamodb.TableV2(this, 'TestTable', {
tableName: 'floci-cdk-test-table',
partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
// DynamoDB table with GSI and LSI — validates CloudFormation index provisioning
const indexTable = new dynamodb.TableV2(this, 'IndexTestTable', {
tableName: 'floci-cdk-index-table',
partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
sortKey: { name: 'sk', type: dynamodb.AttributeType.STRING },
removalPolicy: cdk.RemovalPolicy.DESTROY,
globalSecondaryIndexes: [
{
indexName: 'gsi-1',
partitionKey: { name: 'gsiPk', type: dynamodb.AttributeType.STRING },
sortKey: { name: 'sk', type: dynamodb.AttributeType.STRING },
projectionType: dynamodb.ProjectionType.ALL,
},
],
localSecondaryIndexes: [
{
indexName: 'lsi-1',
sortKey: { name: 'lsiSk', type: dynamodb.AttributeType.STRING },
projectionType: dynamodb.ProjectionType.KEYS_ONLY,
},
],
});
const generatedSecret = new secretsmanager.CfnSecret(this, 'GeneratedSecret', {
name: 'floci-cdk-generated-secret',
generateSecretString: {
secretStringTemplate: '{"username":"admin"}',
generateStringKey: 'password',
passwordLength: 24,
excludeCharacters: 'abc',
},
});
generatedSecret.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
// Custom Docker image Lambda — exercises ECR emulation end-to-end:
// CDK builds the local Dockerfile, cdk-assets pushes it to the bootstrap
// ECR repository (via Floci's emulated ECR + registry:2), and Floci's
// Lambda runner pulls the image at invoke time.
new lambda.DockerImageFunction(this, 'DockerHelloFn', {
functionName: 'floci-cdk-docker-hello',
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, '..', 'docker-fn')),
timeout: cdk.Duration.seconds(15),
memorySize: 256,
});
// VPC + internal ALB — regression for issue #1297: CloudFormation must create the
// EC2 VPC/subnets for real (in EC2), otherwise the ALB that references the
// CDK-generated subnets fails with "The subnet ID '...' does not exist".
const vpc = new ec2.Vpc(this, 'TestVpc', {
vpcName: 'floci-cdk-vpc',
maxAzs: 2,
natGateways: 0,
subnetConfiguration: [
{ name: 'public', subnetType: ec2.SubnetType.PUBLIC, cidrMask: 24 },
{ name: 'isolated', subnetType: ec2.SubnetType.PRIVATE_ISOLATED, cidrMask: 24 },
],
});
const alb = new elbv2.ApplicationLoadBalancer(this, 'TestAlb', {
loadBalancerName: 'floci-cdk-alb',
vpc,
internetFacing: false,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_ISOLATED },
});
// Cloud Map namespace + service: regression for https://github.com/floci-io/floci/issues/4596.
const namespace = new servicediscovery.PrivateDnsNamespace(this, 'TestNamespace', {
name: 'floci-cdk.internal',
vpc,
});
const service = namespace.createService('TestService', {
name: 'floci-cdk-svc',
dnsRecordType: servicediscovery.DnsRecordType.A,
dnsTtl: cdk.Duration.seconds(30),
});
new cdk.CfnOutput(this, 'BucketName', { value: bucket.bucketName });
new cdk.CfnOutput(this, 'QueueUrl', { value: queue.queueUrl });
new cdk.CfnOutput(this, 'TableName', { value: table.tableName });
new cdk.CfnOutput(this, 'IndexTableName', { value: indexTable.tableName });
new cdk.CfnOutput(this, 'GeneratedSecretName', { value: generatedSecret.name || 'floci-cdk-generated-secret' });
new cdk.CfnOutput(this, 'VpcId', { value: vpc.vpcId });
new cdk.CfnOutput(this, 'AlbArn', { value: alb.loadBalancerArn });
new cdk.CfnOutput(this, 'NamespaceId', { value: namespace.namespaceId });
new cdk.CfnOutput(this, 'ServiceId', { value: service.serviceId });
}
}