AWS 扩展构造库
AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
关于如何移徙的更多信息,请参见: 移动到 AWS CDK v2 指南。 。 。 .
AWS 扩展控制台提供了一个基于 Git 的工作流程,用于部署和托管 Fullstack 服务器无网络应用程序. 一个fullstack服务器无端应用包括用GraphQL或REST API等云资源所构建的后端,文件和数据存储,以及用React,Angular,Vue等单页应用框架所构建的前端.
设置包含分支、自定义规则和域名的应用程序
设置 Amplify Console 应用程序,定义 App数字 :
import * as codebuild from '@aws-cdk/aws-codebuild';
const amplifyApp = new amplify.App(this, 'MyApp', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: '<user>',
repository: '<repo>',
oauthToken: SecretValue.secretsManager('my-github-token'),
}),
buildSpec: codebuild.BuildSpec.fromObjectToYaml({
// Alternatively add a `amplify.yml` to the repo
version: '1.0',
frontend: {
phases: {
preBuild: {
commands: [
'yarn',
],
},
build: {
commands: [
'yarn build',
],
},
},
artifacts: {
baseDirectory: 'public',
files:
- '**/*',
},
},
}),
});
连接您的 App 到 GitLab,请使用 GitLabSourceCodeProvider数字 :
const amplifyApp = new amplify.App(this, 'MyApp', {
sourceCodeProvider: new amplify.GitLabSourceCodeProvider({
owner: '<user>',
repository: '<repo>',
oauthToken: SecretValue.secretsManager('my-gitlab-token'),
}),
});
连接您的 App 代码承诺,使用 CodeCommitSourceCodeProvider数字 :
import * as codecommit from '@aws-cdk/aws-codecommit';
const repository = new codecommit.Repository(this, 'Repo', {
repositoryName: 'my-repo',
});
const amplifyApp = new amplify.App(this, 'App', {
sourceCodeProvider: new amplify.CodeCommitSourceCodeProvider({ repository }),
});
机构间委员会的作用 App 将自动获得许可
以拉出代码承诺存储器.
添加分支 :
declare const amplifyApp: amplify.App;
const master = amplifyApp.addBranch('master'); // `id` will be used as repo branch name
const dev = amplifyApp.addBranch('dev', {
performanceMode: true, // optional, enables performance mode
});
dev.addEnvironment('STAGE', 'dev');
默认启用自动构建和拉动请求预览 .
添加重定向的自定义规则 :
declare const amplifyApp: amplify.App;
amplifyApp.addCustomRule({
source: '/docs/specific-filename.html',
target: '/documents/different-filename.html',
status: amplify.RedirectStatus.TEMPORARY_REDIRECT,
});
当使用单个页面应用程序( SPA) 时, 请使用
CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT 设立200
改写所有文件 index.html 除外:
文件扩展名:cs, gif, ico, jpg, js, png, txt, svg, woff,
ttf,地图,json,网络侠客.
declare const mySinglePageApp: amplify.App;
mySinglePageApp.addCustomRule(amplify.CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT);
向分支添加一个域并映射子域:
declare const amplifyApp: amplify.App;
declare const master: amplify.Branch;
declare const dev: amplify.Branch;
const domain = amplifyApp.addDomain('example.com', {
enableAutoSubdomain: true, // in case subdomains should be auto registered for branches
autoSubdomainCreationPatterns: ['*', 'pr*'], // regex for branches that should auto register subdomains
});
domain.mapRoot(master); // map master branch to domain root
domain.mapSubDomain(master, 'www');
domain.mapSubDomain(dev); // sub domain prefix defaults to branch name
限制进入
密码通过指定 : basicAuth 道具 (英语).
使用 BasicAuth.fromCredentials 当引用现有机密时:
const amplifyApp = new amplify.App(this, 'MyApp', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: '<user>',
repository: '<repo>',
oauthToken: SecretValue.secretsManager('my-github-token'),
}),
basicAuth: amplify.BasicAuth.fromCredentials('username', SecretValue.secretsManager('my-github-token')),
});
使用 BasicAuth.fromGeneratedPassword 在密件管理器中生成密码 :
const amplifyApp = new amplify.App(this, 'MyApp', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: '<user>',
repository: '<repo>',
oauthToken: SecretValue.secretsManager('my-github-token'),
}),
basicAuth: amplify.BasicAuth.fromGeneratedPassword('username'),
});
可以在特定的分支中添加基本的认证 :
declare const amplifyApp: amplify.App;
amplifyApp.addBranch('feature/next', {
basicAuth: amplify.BasicAuth.fromGeneratedPassword('username'),
});
自动创建和删除分支
使用 autoBranchCreation 财务报告和审定财务报表 autoBranchDeletion 控制创建/删除的道具
分支:
const amplifyApp = new amplify.App(this, 'MyApp', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: '<user>',
repository: '<repo>',
oauthToken: SecretValue.secretsManager('my-github-token'),
}),
autoBranchCreation: { // Automatically connect branches that match a pattern set
patterns: ['feature/*', 'test/*'],
},
autoBranchDeletion: true, // Automatically disconnect a branch when you delete a branch from your repository
});
添加自定义响应头
使用 customResponseHeaders 为 Amplify 应用程序配置自定义响应头的道具 :
const amplifyApp = new amplify.App(this, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: '<user>',
repository: '<repo>',
oauthToken: SecretValue.secretsManager('my-github-token'),
}),
customResponseHeaders: [
{
pattern: '*.json',
headers: {
'custom-header-name-1': 'custom-header-value-1',
'custom-header-name-2': 'custom-header-value-2',
},
},
{
pattern: '/path/*',
headers: {
'custom-header-name-1': 'custom-header-value-2',
},
},
],
});
部署资产
sourceCodeProvider 是可选的; 当未指定时, 可使用 Amplify 应用程序 .zip 软件包。 这个 asset 属性可用于将 S3 资产部署到 Amplify 作为 CDK 的一部分:
import * as assets from '@aws-cdk/aws-s3-assets';
declare const asset: assets.Asset;
declare const amplifyApp: amplify.App;
const branch = amplifyApp.addBranch("dev", { asset: asset });