Chúng ta sẽ cần thêm GitHub Personal Access Token vào AWS Secrets Manager của AWS để tận dụng AWS CodePipeline và GitHub vì pipeline của chúng ta sẽ tận dụng webhook để chạy thành công.
Bạn có thể tham khảo thêm về cách tạo GitHub Personal Access Token
Sau khi tạo GitHub Personal Access Token
aws secretsmanager create-secret --name "eks-workshop-token" --description "github access token" --secret-string "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"
Lưu ý: nhớ thay secret-string của bạn bằng token bạn đã tạo.

Chúng ta có thể tạo một tài nguyên CodePipelineStack mới bằng cách tạo một CDK Construct mới trong thư mục lib/, sau đó nhập Construct vào main entrypoint file.
touch lib/pipeline.ts

// lib/pipeline.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as blueprints from '@aws-quickstart/eks-blueprints';
import { KubernetesVersion } from 'aws-cdk-lib/aws-eks';
export default class PipelineConstruct extends Construct {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id)
const account = props?.env?.account!;
const region = props?.env?.region!;
const blueprint = blueprints.EksBlueprint.builder()
.account(account)
.region(region)
.clusterProvider(
new blueprints.GenericClusterProvider({
version: 'auto'
})
)
.addOns()
.teams();
blueprints.CodePipelineStack.builder()
.name("eks-blueprints-workshop-pipeline")
.owner("your-github-username")
.repository({
repoUrl: 'your-repo-name',
credentialsSecretName: 'github-token',
targetRevision: 'main'
})
.build(scope, id+'-stack', props);
}
}
Thực hiện cấu hình:

Để đảm bảo chúng ta có thể truy cập vào Construct, chúng ta cần import và khởi tạo một construct mới.
// bin/my-eks-blueprints.ts
// bin/my-eks-blueprints.ts
import * as cdk from 'aws-cdk-lib';
import ClusterConstruct from '../lib/my-eks-blueprints-stack';
import * as dotenv from 'dotenv';
import PipelineConstruct from '../lib/pipeline'; // IMPORT OUR PIPELINE CONSTRUCT
dotenv.config();
const app = new cdk.App();
const account = process.env.CDK_DEFAULT_ACCOUNT!;
const region = process.env.CDK_DEFAULT_REGION;
const env = { account, region }
new ClusterConstruct(app, 'cluster', { env });
new PipelineConstruct(app, 'pipeline', { env });

cdk list

// lib/pipeline.ts
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as blueprints from '@aws-quickstart/eks-blueprints';
import { KubernetesVersion } from 'aws-cdk-lib/aws-eks';
export default class PipelineConstruct extends Construct {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id)
const account = props?.env?.account!;
const region = props?.env?.region!;
const blueprint = blueprints.EksBlueprint.builder()
.account(account)
.region(region)
.clusterProvider(
new blueprints.GenericClusterProvider({
version: 'auto'
})
)
.addOns()
.teams();
blueprints.CodePipelineStack.builder()
.name("eks-blueprints-workshop-pipeline")
.owner("your-github-username")
.repository({
repoUrl: 'your-repo-name',
credentialsSecretName: 'github-token',
targetRevision: 'main'
})
// WE ADD THE STAGES IN WAVE FROM THE PREVIOUS CODE
.wave({
id: "envs",
stages: [
{ id: "dev", stackBuilder: blueprint.clone('ap-southeast-1') }
]
})
.build(scope, id + '-stack', props);
}
}
Sử dụng class blueprints.StackStage hỗ trợ tạo để định nghĩa các stage của chúng ta bằng cách sử dụng .stage
Sử dụng .wave hỗ trợ để triển khai song song.
Trong bài lab, chúng ta đang triển khai 1 cluster.
Nếu trường hợp bạn triển khai nhiều cluster, để giảm thiểu, chúng ta sẽ chỉ cần thêm .wave vào danh sách các stage để bao gồm cách bạn muốn cấu trúc các stage triển khai khác nhau của mình trong pipeline. (tức là khác nhau add-ons, region deployment v.v.).
Stack của chúng ta sẽ triển khai các cluster sau: EKS trong môi trường dev. CodePipeline triển khai tới region: ap-southeast-1.

cdk list
Kết quả như sau:
cluster-stack
pipeline-stack
pipeline-stack/dev/dev-blueprint
