Package: cdk8s-awscdk-resolver
AWS CDK ResolverTheAwsCdkResolveris able to resolve anyCfnOutputdefined by your AWS CDK application. In this example, we create an S3Bucketwith the AWS CDK, and pass its (deploy time generated)
name as an environment variable to a KubernetesCronJobresource.import*asawsfrom'aws-cdk-lib';import*ask8sfrom'cdk8s';import*askplusfrom'cdk8s-plus-27';import{AwsCdkResolver}from'@cdk8s/awscdk-resolver';constawsApp=newaws.App();conststack=newaws.Stack(awsApp,'aws');constk8sApp=newk8s.App({resolvers:[newAwsCdkResolver()]});constmanifest=newk8s.Chart(k8sApp,'Manifest');constbucket=newaws.aws_s3.Bucket(stack,'Bucket');constbucketName=newaws.CfnOutput(stack,'BucketName',{value:bucket.bucketName,});newkplus.CronJob(manifest,'CronJob',{schedule:k8s.Cron.daily(),containers:[{image:'job',envVariables:{//directlypassingthevalueofthe`CfnOutput`containing//thedeploytimebucketnameBUCKET_NAME:kplus.EnvValue.fromValue(bucketName.value),}}]});awsApp.synth();k8sApp.synth();During cdk8s synthesis, the custom resolver will detect thatbucketName.valueis not a concrete value,
but rather a value of aCfnOutput. It will then perform AWS service calls in order to fetch the
actual value from the deployed infrastructure in your account. This means that in order
forcdk8s synthto succeed, it must be executedafterthe AWS CDK resources
have been deployed. So your deployment workflow should (conceptually) be:cdk deploycdk8s synthNote that theAwsCdkResolverisonlyable to fetch tokens that have aCfnOutputdefined for them.PermissionsSince runningcdk8s synthwill now require performing AWS service calls, it must have access
to a set of AWS credentials. Following are the set of actions the credentials must allow:cloudformation:DescribeStacksNote that the actions cdk8s require are far more scoped down than those normally required for the
deployment of AWS CDK applications. It is therefore recommended to not reuse the same set of credentials,
and instead create a scoped downReadOnlyrole dedicated for cdk8s resolvers.Cross Repository WorkflowAs we've seen, yourcdk8sapplication needs access to the objects defined in your cloud application. If both applications
are defined within the same file, this is trivial to achieve. If they are in different files, a simpleimportstatement will suffice.
However, what if the applications are managed in two separate repositories? This makes it a little trickier, but still possible.In this scenario,cdk.tsin the AWS CDK application, stored in a dedicated repository.import*asawsfrom'aws-cdk-lib';constawsApp=newaws.App();conststack=newaws.Stack(awsApp,'aws');constbucket=newaws.aws_s3.Bucket(stack,'Bucket');constbucketName=newaws.CfnOutput(stack,'BucketName',{value:bucket.bucketName,});awsApp.synth();In order for thecdk8sapplication to have cross repository access, the AWS CDK object instances that we want to expose need to be available
via a package repository. To do this, break up the AWS CDK application into the following files:app.tsimport*asawsfrom'aws-cdk-lib';constawsApp=newaws.App();conststack=newaws.Stack(awsApp,'aws');constbucket=newaws.aws_s3.Bucket(stack,'Bucket');//exportthethingwewanttohaveavailableforcdk8sapplicationsexportconstbucketName=newaws.CfnOutput(stack,'BucketName',{value:bucket.bucketName,});//notethatwedon't call awsApp.synth heremain.tsimport{awsApp}from'./app.ts'awsApp.synth();Now, publish theapp.tsfile to a package manager, so that yourcdk8sapplication can install and import it.
This approach might be somewhat counter intuitive, because normally we only publish classes to the package manager,
not instances. Indeed, these types of applications introduce a new use-case that requires the sharing of instances.
Conceptually, this is no different than writing state*to an SSM parameter or an S3 bucket, and it allows us to remain
in the boundaries of our programming language, and the typing guarantees it provides.*Actually, we are only publishing instructions for fetching state, not the state itself.Assumingapp.tswas published as themy-cdk-apppackage, ourcdk8sapplication will now look like so:import*ask8sfrom'cdk8s';import*askplusfrom'cdk8s-plus-27';//importthedesiredinstancefromtheAWSCDKapp.import{bucketName}from'my-cdk-app';import{AwsCdkResolver}from'@cdk8s/awscdk-resolver';constk8sApp=newk8s.App({resolvers:[newAwsCdkResolver()]});constmanifest=newk8s.Chart(k8sApp,'Manifest');newkplus.CronJob(manifest,'CronJob',{schedule:k8s.Cron.daily(),containers:[{image:'job',envVariables:{//directlypassingthevalueofthe`CfnOutput`containing//thedeploytimebucketnameBUCKET_NAME:kplus.EnvValue.fromValue(bucketName.value),}}]});k8sApp.synth();