0.0.192 • Published 2 months ago

@cdk8s/awscdk-resolver v0.0.192

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 months ago

AWS CDK Resolver

The AwsCdkResolver is able to resolve any CfnOutput defined by your AWS CDK application. In this example, we create an S3 Bucket with the AWS CDK, and pass its (deploy time generated) name as an environment variable to a Kubernetes CronJob resource.

import * as aws from 'aws-cdk-lib';
import * as k8s from 'cdk8s';
import * as kplus from 'cdk8s-plus-27';

import { AwsCdkResolver } from '@cdk8s/awscdk-resolver';

const awsApp = new aws.App();
const stack = new aws.Stack(awsApp, 'aws');

const k8sApp = new k8s.App({ resolvers: [new AwsCdkResolver()] });
const manifest = new k8s.Chart(k8sApp, 'Manifest');

const bucket = new aws.aws_s3.Bucket(stack, 'Bucket');
const bucketName = new aws.CfnOutput(stack, 'BucketName', {
  value: bucket.bucketName,
});

new kplus.CronJob(manifest, 'CronJob', {
  schedule: k8s.Cron.daily(),
  containers: [{
    image: 'job',
    envVariables: {
      // directly passing the value of the `CfnOutput` containing 
      // the deploy time bucket name
      BUCKET_NAME: kplus.EnvValue.fromValue(bucketName.value),
    }
 }]
});

awsApp.synth();
k8sApp.synth();

During cdk8s synthesis, the custom resolver will detect that bucketName.value is not a concrete value, but rather a value of a CfnOutput. 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 for cdk8s synth to succeed, it must be executed after the AWS CDK resources have been deployed. So your deployment workflow should (conceptually) be:

  1. cdk deploy
  2. cdk8s synth

Note that the AwsCdkResolver is only able to fetch tokens that have a CfnOutput defined for them.

Permissions

Since running cdk8s synth will 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:DescribeStacks

Note 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 down ReadOnly role dedicated for cdk8s resolvers.

Cross Repository Workflow

As we've seen, your cdk8s application 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 simple import statement 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.ts in the AWS CDK application, stored in a dedicated repository.

import * as aws from 'aws-cdk-lib';

const awsApp = new aws.App();
const stack = new aws.Stack(awsApp, 'aws');

const bucket = new aws.aws_s3.Bucket(stack, 'Bucket');
const bucketName = new aws.CfnOutput(stack, 'BucketName', {
  value: bucket.bucketName,
});

awsApp.synth();

In order for the cdk8s application 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.ts

import * as aws from 'aws-cdk-lib';

const awsApp = new aws.App();
const stack = new aws.Stack(awsApp, 'aws');

const bucket = new aws.aws_s3.Bucket(stack, 'Bucket');
// export the thing we want to have available for cdk8s applications
export const bucketName = new aws.CfnOutput(stack, 'BucketName', {
  value: bucket.bucketName,
});

// note that we don't call awsApp.synth here

main.ts

import { awsApp } from './app.ts'

awsApp.synth();

Now, publish the app.ts file to a package manager, so that your cdk8s application 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.

Assuming app.ts was published as the my-cdk-app package, our cdk8s application will now look like so:

import * as k8s from 'cdk8s';
import * as kplus from 'cdk8s-plus-27';

// import the desired instance from the AWS CDK app.
import { bucketName } from 'my-cdk-app';

import { AwsCdkResolver } from '@cdk8s/awscdk-resolver';

const k8sApp = new k8s.App({ resolvers: [new AwsCdkResolver()] });
const manifest = new k8s.Chart(k8sApp, 'Manifest');

new kplus.CronJob(manifest, 'CronJob', {
  schedule: k8s.Cron.daily(),
  containers: [{
    image: 'job',
    envVariables: {
      // directly passing the value of the `CfnOutput` containing 
      // the deploy time bucket name
      BUCKET_NAME: kplus.EnvValue.fromValue(bucketName.value),
    }
 }]
});

k8sApp.synth();
0.0.192

2 months ago

0.0.191

2 months ago

0.0.190

2 months ago

0.0.189

2 months ago

0.0.188

2 months ago

0.0.187

2 months ago

0.0.175

3 months ago

0.0.174

3 months ago

0.0.179

3 months ago

0.0.178

3 months ago

0.0.177

3 months ago

0.0.176

3 months ago

0.0.186

2 months ago

0.0.185

2 months ago

0.0.184

2 months ago

0.0.183

2 months ago

0.0.182

2 months ago

0.0.181

2 months ago

0.0.180

3 months ago

0.0.173

3 months ago

0.0.159

4 months ago

0.0.158

4 months ago

0.0.157

4 months ago

0.0.156

4 months ago

0.0.155

4 months ago

0.0.169

3 months ago

0.0.164

3 months ago

0.0.163

3 months ago

0.0.162

3 months ago

0.0.161

3 months ago

0.0.168

3 months ago

0.0.167

3 months ago

0.0.166

3 months ago

0.0.165

3 months ago

0.0.160

4 months ago

0.0.172

3 months ago

0.0.171

3 months ago

0.0.170

3 months ago

0.0.153

4 months ago

0.0.152

4 months ago

0.0.151

4 months ago

0.0.150

4 months ago

0.0.154

4 months ago

0.0.106

7 months ago

0.0.105

7 months ago

0.0.109

7 months ago

0.0.108

7 months ago

0.0.107

7 months ago

0.0.117

6 months ago

0.0.116

6 months ago

0.0.115

7 months ago

0.0.114

7 months ago

0.0.119

6 months ago

0.0.118

6 months ago

0.0.113

7 months ago

0.0.112

7 months ago

0.0.111

7 months ago

0.0.110

7 months ago

0.0.128

6 months ago

0.0.127

6 months ago

0.0.126

6 months ago

0.0.125

6 months ago

0.0.129

6 months ago

0.0.120

6 months ago

0.0.124

6 months ago

0.0.123

6 months ago

0.0.122

6 months ago

0.0.121

6 months ago

0.0.139

5 months ago

0.0.138

5 months ago

0.0.137

5 months ago

0.0.136

5 months ago

0.0.131

6 months ago

0.0.130

6 months ago

0.0.135

5 months ago

0.0.134

5 months ago

0.0.133

5 months ago

0.0.132

5 months ago

0.0.149

4 months ago

0.0.148

4 months ago

0.0.147

4 months ago

0.0.142

4 months ago

0.0.141

5 months ago

0.0.140

5 months ago

0.0.146

4 months ago

0.0.145

4 months ago

0.0.144

4 months ago

0.0.143

4 months ago

0.0.104

7 months ago

0.0.103

7 months ago

0.0.102

7 months ago

0.0.101

7 months ago

0.0.100

7 months ago

0.0.99

8 months ago

0.0.98

8 months ago

0.0.97

8 months ago

0.0.96

8 months ago

0.0.95

8 months ago

0.0.94

8 months ago

0.0.93

8 months ago

0.0.91

8 months ago

0.0.92

8 months ago

0.0.90

8 months ago

0.0.89

8 months ago

0.0.88

8 months ago

0.0.87

8 months ago

0.0.86

8 months ago

0.0.85

8 months ago

0.0.84

8 months ago

0.0.83

8 months ago

0.0.82

8 months ago

0.0.80

9 months ago

0.0.81

9 months ago

0.0.79

9 months ago

0.0.78

9 months ago

0.0.77

9 months ago

0.0.76

9 months ago

0.0.75

9 months ago

0.0.66

10 months ago

0.0.67

10 months ago

0.0.65

10 months ago

0.0.64

10 months ago

0.0.63

10 months ago

0.0.62

10 months ago

0.0.61

10 months ago

0.0.60

10 months ago

0.0.59

10 months ago

0.0.58

10 months ago

0.0.57

11 months ago

0.0.56

11 months ago

0.0.54

11 months ago

0.0.55

11 months ago

0.0.52

11 months ago

0.0.53

11 months ago

0.0.51

11 months ago

0.0.50

11 months ago

0.0.49

12 months ago

0.0.48

12 months ago

0.0.47

12 months ago

0.0.46

12 months ago

0.0.45

1 year ago

0.0.44

1 year ago

0.0.43

1 year ago

0.0.42

1 year ago

0.0.40

1 year ago

0.0.41

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.34

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago