-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from dannysteenman/feat/refactor-oidc
feat(stacks): refactor project structure and enhance resource naming
- Loading branch information
Showing
12 changed files
with
229 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,101 @@ | ||
# AWS CDK Stacks: BaseStack and GitHubOIDCStack | ||
# AWS CDK Stacks: StarterStack and FoundationStack | ||
|
||
This documentation details the structure and functionality of two pivotal stacks within our AWS CDK TypeScript project: `BaseStack` and `GitHubOIDCStack`. These stacks lay the groundwork for deploying AWS resources with specific configurations and capabilities, tailored to different deployment stages and integration with GitHub Actions for CI/CD processes. | ||
This documentation details the structure and functionality of three pivotal stacks within our AWS CDK TypeScript project: `StarterStack` and `FoundationStack`. These stacks lay the groundwork for deploying AWS resources with specific configurations and capabilities, tailored to different deployment stages and integration with GitHub Actions for CI/CD processes. | ||
|
||
## BaseStack | ||
## StarterStack | ||
|
||
The `BaseStack` serves as a foundational stack that you can use to start instantiation your custom and cdk-lib constructs. | ||
The `StarterStack` serves as a foundation for building your AWS infrastructure using CDK. It provides a starting point for adding and organizing your AWS resources and constructs. | ||
|
||
### Properties | ||
|
||
- `environment`: Optional. Specifies the deployment stage (e.g., `dev`, `test`, `staging`, `production`). It's crucial for tailoring the stack configuration to the target environment. | ||
- `environment`: Optional. Specifies the deployment stage (e.g., `dev`, `test`, `staging`, `production`). If not provided, an error will be thrown. | ||
|
||
### Features | ||
|
||
- Includes a `NetworkConstruct` that creates a secure VPC. | ||
- Designed to be easily extendable with additional AWS resources and constructs. | ||
|
||
### Usage | ||
|
||
The `StarterStack` is intended to be customized and extended based on your specific infrastructure needs. You can add new constructs and resources within the constructor of this class. | ||
|
||
### Example Usage | ||
|
||
```typescript | ||
import * as cdk from 'aws-cdk-lib'; | ||
import { BaseStack } from './stacks'; | ||
import { StarterStack } from './stacks'; | ||
|
||
// Inherit environment variables from npm run commands (displayed in .projen/tasks.json) | ||
const environment = process.env.ENVIRONMENT || 'dev'; | ||
const awsEnvironment = { | ||
account: process.env.CDK_DEFAULT_ACCOUNT, | ||
region: process.env.CDK_DEFAULT_REGION, | ||
}; | ||
|
||
// Instantiate the CDK app | ||
const app = new cdk.App(); | ||
|
||
// Create a new stack with your resources | ||
new BaseStack(app, `BaseStack-${environment}`, { | ||
new StarterStack(app, `StarterStack-${environment}`, { | ||
env: awsEnvironment, | ||
environment: environment, | ||
}); | ||
``` | ||
|
||
## GitHubOIDCStack | ||
### Extending the Stack | ||
|
||
The GitHubOIDCStack is designed to facilitate secure CI/CD workflows by integrating AWS resources with GitHub Actions via OpenID Connect (OIDC). This allows for a more secure and streamlined deployment process directly from GitHub Actions. | ||
To add new resources to the `StarterStack`, you can modify the constructor. For example, to add a new S3 bucket: | ||
|
||
```typescript | ||
import * as s3 from 'aws-cdk-lib/aws-s3'; | ||
|
||
// In the constructor: | ||
new s3.Bucket(this, 'MyBucket', { | ||
bucketName: 'my-unique-bucket-name', | ||
versioned: true, | ||
encryption: s3.BucketEncryption.S3_MANAGED, | ||
}); | ||
``` | ||
|
||
### Best Practices | ||
|
||
- Organize your constructs logically within this stack. | ||
- For complex infrastructures, consider creating additional stacks for different components. | ||
- Remember to import necessary modules at the top of the file. | ||
- Customize and extend the stack based on your specific infrastructure requirements. | ||
|
||
The `StarterStack` provides a flexible foundation for your AWS CDK project, allowing you to incrementally build and organize your infrastructure as code. | ||
|
||
|
||
## FoundationStack | ||
|
||
The `FoundationStack` sets up fundamental infrastructure components for AWS deployments via GitHub Actions. It combines the functionality of the GitHubOIDCStack with additional features. | ||
|
||
### Features | ||
|
||
- GitHub Actions OIDC Provider: Sets up the OIDC provider for GitHub Actions within the AWS account, enabling trust relationships between GitHub and AWS. | ||
- GitHub Deploy Role: Creates an IAM role with AdministratorAccess managed policy. This role is assumable by GitHub Actions workflows, granting them the permissions needed to deploy resources. | ||
- GitHub Actions OIDC Provider: Sets up the OIDC provider for GitHub Actions within the AWS account. | ||
- GitHub Deploy Role: Creates an IAM role with AdministratorAccess managed policy, assumable by GitHub Actions workflows. | ||
- CDK Toolkit Cleaner: Implements a ToolkitCleaner for managing CDK toolkit resources. | ||
|
||
### Configuration | ||
### Properties | ||
|
||
The stack automatically retrieves GitHub repository details (owner and repository name) using a helper function, ensuring that the OIDC provider and IAM roles are correctly configured for the specific GitHub repository. | ||
- `environment`: Required. Specifies the deployment stage (e.g., `dev`, `test`, `staging`, `production`). | ||
|
||
### Example Usage | ||
|
||
```typescript | ||
import * as cdk from 'aws-cdk-lib'; | ||
import { GitHubOIDCStack } from './stacks'; | ||
import { FoundationStack } from './stacks'; | ||
|
||
// Inherit environment variables from npm run commands (displayed in .projen/tasks.json) | ||
const environment = process.env.ENVIRONMENT || 'dev'; | ||
const awsEnvironment = { | ||
account: process.env.CDK_DEFAULT_ACCOUNT, | ||
region: process.env.CDK_DEFAULT_REGION, | ||
}; | ||
|
||
// Instantiate the CDK app | ||
const app = new cdk.App(); | ||
|
||
// Add GitHub OpenID Connect support and create an IAM role for GitHub | ||
new GitHubOIDCStack(app, `GitHubOIDCStack-${environment}`, { | ||
new FoundationStack(app, `FoundationStack-${environment}`, { | ||
env: awsEnvironment, | ||
environment: environment, | ||
}); | ||
``` | ||
|
||
The `FoundationStack` provides a more comprehensive setup for GitHub Actions integration and CDK resource management, making it suitable for projects that require both OIDC-based deployments and CDK toolkit cleaning. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as cdk from 'aws-cdk-lib'; | ||
import * as iam from 'aws-cdk-lib/aws-iam'; | ||
import { ToolkitCleaner } from 'cloudstructs/lib/toolkit-cleaner'; | ||
import type { Construct } from 'constructs'; | ||
import { getGitRepositoryDetails } from '../bin/git-helper'; | ||
|
||
export interface FoundationStackProps extends cdk.StackProps { | ||
/** | ||
* Determine the stage to which you want to deploy the stack | ||
* | ||
* @default - If not given, it will throw out an error | ||
*/ | ||
readonly environment: string; | ||
} | ||
|
||
/** | ||
* FoundationStack | ||
* | ||
* This stack sets up fundamental infrastructure components for AWS deployments via GitHub Actions. | ||
* It includes the creation of an OpenID Connect (OIDC) provider for GitHub and an IAM role for | ||
* GitHub Actions deployments. | ||
* | ||
* @extends cdk.Stack | ||
* | ||
* @remarks | ||
* - Creates a GitHub OIDC provider | ||
* - Sets up an IAM role for GitHub Actions with necessary permissions | ||
* - Implements a ToolkitCleaner for managing CDK toolkit resources | ||
* | ||
* @param scope - The scope in which to define this construct | ||
* @param id - The scoped construct ID | ||
* @param props - Stack properties | ||
*/ | ||
export class FoundationStack extends cdk.Stack { | ||
constructor(scope: Construct, id: string, props: FoundationStackProps) { | ||
super(scope, id, props); | ||
|
||
//////////////////////////////// | ||
// Setup GitHub OIDC support // | ||
////////////////////////////// | ||
const { gitOwner, gitRepoName } = getGitRepositoryDetails(); | ||
|
||
const githubDomain = 'token.actions.githubusercontent.com'; | ||
|
||
const openIdConnectProvider = new iam.OpenIdConnectProvider(this, 'GithubProvider', { | ||
url: `https://${githubDomain}`, | ||
clientIds: ['sts.amazonaws.com'], | ||
}); | ||
|
||
const conditions: iam.Conditions = { | ||
StringLike: { | ||
[`${githubDomain}:sub`]: `repo:${gitOwner}/${gitRepoName}:environment:${props.environment}`, | ||
}, | ||
StringEquals: { | ||
[`${githubDomain}:aud`]: 'sts.amazonaws.com', | ||
}, | ||
}; | ||
|
||
new iam.Role(this, 'GitHubActionsServiceRole', { | ||
assumedBy: new iam.WebIdentityPrincipal(openIdConnectProvider.openIdConnectProviderArn, conditions), | ||
description: 'This role is used via GitHub Actions to deploy with AWS CDK or Terraform on the target AWS account', | ||
managedPolicies: [iam.ManagedPolicy.fromAwsManagedPolicyName('AdministratorAccess')], | ||
maxSessionDuration: cdk.Duration.hours(2), | ||
roleName: process.env.GITHUB_DEPLOY_ROLE ?? 'GitHubActionsServiceRole', | ||
}); | ||
|
||
//////////////////////////////// | ||
// Setup CDK Toolkit Cleaner // | ||
////////////////////////////// | ||
new ToolkitCleaner(this, 'ToolkitCleaner'); | ||
} | ||
} |
Oops, something went wrong.