@fntio/infra-client
Version:
Self-hosted AWS access portal: credential vending, target-account trust, and grant-driven private networking as CDK stacks and constructs.
81 lines (80 loc) • 3.77 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.mirroredImage = mirroredImage;
exports.taskImage = taskImage;
const cdk = __importStar(require("aws-cdk-lib"));
const ecr = __importStar(require("aws-cdk-lib/aws-ecr"));
const ecs = __importStar(require("aws-cdk-lib/aws-ecs"));
/** Loaded only when mirroring is on: the package (about 50 MB, with its own `constructs` floor) is an
* optional peer, so deployments that leave the option off install nothing extra. */
function copyTool() {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require("cdk-ecr-deployment");
}
catch {
throw new Error(`mirrorImages needs the "cdk-ecr-deployment" package: run "npm install cdk-ecr-deployment" in your deployment project.`);
}
}
/** A copy of a public image (`host/path:tag`) in this account's own registry, made once at deploy. A task
* that starts later (a restart, a replacement) then needs only AWS, not the public registry being reachable
* at that moment. Every task definition using `image` must depend on `copy`, or its service can start a
* task before the image is there. */
function mirroredImage(scope, id, source) {
const tag = source.slice(source.lastIndexOf(":") + 1);
const ecrdeploy = copyTool();
const repository = new ecr.Repository(scope, `${id}Repo`, {
imageScanOnPush: true,
// Not IMMUTABLE: a rollback re-runs the copy onto a tag that already exists, and a refused
// overwrite there would strand the stack in UPDATE_ROLLBACK_FAILED. The tag is our own pin.
lifecycleRules: [{ maxImageCount: 5 }],
removalPolicy: cdk.RemovalPolicy.DESTROY,
emptyOnDelete: true,
});
const copy = new ecrdeploy.ECRDeployment(scope, `${id}Copy`, {
src: new ecrdeploy.DockerImageName(source),
dest: new ecrdeploy.DockerImageName(repository.repositoryUriForTag(tag)),
});
return { image: ecs.ContainerImage.fromEcrRepository(repository, tag), copy };
}
/** The image for one task definition: straight from its public registry, or mirrored. */
function taskImage(scope, id, source, mirror, taskDefinition) {
if (!mirror)
return ecs.ContainerImage.fromRegistry(source);
const mirrored = mirroredImage(scope, id, source);
taskDefinition.node.addDependency(mirrored.copy);
return mirrored.image;
}