hal*_*fer 1 amazon-web-services amazon-route53 aws-lambda aws-cdk
我有一些使用 CDK 创建 API 网关和 Lambda 的 TypeScript 代码。它可以工作并部署到标准 AWS URL。到目前为止,一切都很好。
我现在需要转移 API 网关,以便它在自定义域上运行,以便它可以在 Web 应用程序中设置 cookie。事实证明,这要困难得多,我怀疑我遇到了困难,因为我同时对 TypeScript、AWS 和 CDK 都是新手。网络上有许多文档资源,但大多数都要求我重写我拥有的宝贵的少量工作代码,但我不愿意这样做。
我已经手动创建了一个证书,因为这需要验证,因此在代码中创建它没有意义。除此之外,我希望所有其他资源都由堆栈中的 CDK 代码创建。在我看来,如果我必须手动配置,就违背了 CDK 的目的。
下面的代码部署了我需要的一切gatekeeper.d.aws.example.com- HostedZone、ARecord、LambdaRestApi 和 Function (lambda)。但是它不起作用,因为新分配的 NS 记录与gatekeeper.d.aws.example.com父记录中的记录不匹配d.aws.example.com。
我认为这意味着虽然d.aws.example.com是“已知”的,但gateway子域不能委托给它。
这是我的工作代码:
// Create the lambda resource
const referrerLambda = new lambda.Function(this, 'EisReferrerLambda', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, '../../src/lambda')),
environment: env
});
// Set up the domain name on which the API should appear
const domainName = 'gatekeeper.d.aws.example.com';
// TODO need to fetch it with an env var? Or read from environment?
const certificateArn = 'arn:aws:acm:us-east-1:xxx:certificate/yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy';
const certificate = acm.Certificate.fromCertificateArn(this, 'SslCertificate', certificateArn);
const hostedZone = new route53.HostedZone(this, 'EisReferrerHostedZone', {
zoneName: domainName
});
// Add an A record
new route53.ARecord(this, 'DnsRecord', {
zone: hostedZone,
target: route53.RecordTarget.fromAlias(new targets.ApiGateway(apiGateway)),
});
// I think I need a DomainNameOptions object
const dno : DomainNameOptions = { certificate, domainName };
// Create the APIG resource
// See https://intro-to-cdk.workshop.aws/the-workshop/4-create-apigateway.html
const apiGateway = new apigw.LambdaRestApi(this, "EisReferrerApi", {
handler: referrerLambda,
// proxy = on means that the lambda handles all requests to the APIG,
// instead of just explicit resource endpoints
proxy: false,
// deploy = on means that we get a default stage of "prod", I don't want
// that - I'm creating a custom Deployment anyway
deploy: false,
// Point to a domain name options object
domainName: dno
});
// Create an endpoint in the APIG
// https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigateway-readme.html#defining-apis
const items = apiGateway.root.addResource('gatekeeper');
items.addMethod('GET'); // GET /default/gatekeeper
// The deployment resource is just needed by the Stage system
const deployment = new apigw.Deployment(
this,
'EisReferrerDeployment',
{ api: apiGateway }
);
// Create a Stage (this affects the first component in the path
const stageName = 'default';
apiGateway.deploymentStage = new apigw.Stage(
this,
stageName,
{ deployment, stageName }
);
Run Code Online (Sandbox Code Playgroud)
从代码中可以看出,我已经找到了如何创建 A 记录,但是创建/修改 NS 记录似乎更难。首先,似乎没有 NSRecord 类,至少基于从我的 IDE 自动完成功能探索类结构。
一个基本的解决方案允许我使用在其他地方(在“拥有”该域的 AWS 账户中)设置的固定值创建 NS 记录。更好的解决方案是读取这些记录是什么,然后使用它们。
为了看看我的想法是否正确,我运行了此部署代码,并手动修改了 HostedZone 中自动分配的 NS 记录,以匹配父级(另一个帐户中)中的记录。我想我必须等待这个变化渗透到 DNS 系统中,我会更新结果。
我的手动调整不起作用。因此,我找到了一个新的尝试(请参阅“将 NS 记录添加到不同帐户中的 HostedZone”):
// Commented out from earlier code
// const hostedZone = new route53.HostedZone(this, 'EisReferrerHostedZone', {
// zoneName: domainName
// });
// In the account containing the HostedZone
const parentZone = new route53.PublicHostedZone(this, 'HostedZone', {
zoneName: 'd.aws.example.com',
crossAccountZoneDelegationPrincipal: new iam.AccountPrincipal('12345678012')
});
// In this account
const subZone = new route53.PublicHostedZone(this, 'SubZone', {
zoneName: domainName
});
new route53.CrossAccountZoneDelegationRecord(this, 'delegate', {
delegatedZone: subZone,
parentHostedZoneId: parentZone.hostedZoneId,
delegationRole: parentZone.crossAccountDelegationRole
});
Run Code Online (Sandbox Code Playgroud)
这听起来正是我所需要的,但我担心 AWS 文档在这里已经过时 -crossAccountDelegationRole在我的 IDE 中呈现为红色,并且由于cdk diff运行时未定义而崩溃。
我假设上面提到的属性是一个拼写错误或对过时版本的库的引用。我现在正在这样做:
new route53.CrossAccountZoneDelegationRecord(this, 'delegate', {
delegatedZone: subZone,
parentHostedZoneId: parentZone.hostedZoneId,
delegationRole: parentZone.crossAccountZoneDelegationRole
});
Run Code Online (Sandbox Code Playgroud)
这感觉非常接近,但它崩溃了:
创建资源失败。AccessDenied:用户:arn:aws:sts::xxxxxxxxxxxx:assumed-role/CustomCrossAccountZoneDelegationC-xxx 无权对资源执行:sts:AssumeRole:arn:aws:iam::yyyyyyyyyyyy:role/HostedZoneCrossAccountZoneDelegat-yyy
我想知道是否需要声明另一个账户的 IAM 凭证?我确实有它们。
无论如何,我不确定为什么需要权限 - 它不能只是读取其他帐户中的 NS 记录并将其复制到本地帐户吗?无论如何,另一个帐户中的 DNS 是公开的。
我愿意研究修复 IAM 错误,但这并不像在黑暗中拍摄。我可能会再花两个小时慢慢解决这个子问题,却发现整个事情会因为另一个原因而失败。
我在远程账户中创建了一个“角色”,为我要部署 CDK 的账户提供“AmazonRoute53FullAccess”权限。但是我仍然收到 AccessDenied 错误。我想知道我是否需要以某种方式显式调用该远程角色;我怎样才能做到这一点?
小智 10
今天想做同样的事情,你的帖子让我完成了 90%,谢谢!我最终让它与不同的 IAM 主体(组织)一起工作,这对于我的用例来说是可以的。
crossAccountZoneDelegationPrincipal 提供对托管子区域的帐户的访问权限,以访问您的根区域并写入子区域的委派 (NS) 记录。
对于我的用例,所有帐户都位于同一组织内,因此我创建了我的根区域,如下所示 ->
const rootZone = new route53.PublicHostedZone(this, 'rootZone', {
zoneName: `root.zone`,
crossAccountZoneDelegationPrincipal: new iam.OrganizationPrincipal('o-####')
});
Run Code Online (Sandbox Code Playgroud)
这将使用以下策略设置 IAM 角色;
"Version": "2012-10-17",
"Statement": [
{
"Action": "route53:ChangeResourceRecordSets",
"Resource": "arn:aws:route53:::hostedzone/#####",
"Effect": "Allow"
}
]
}
Run Code Online (Sandbox Code Playgroud)
以及以下信托政策;
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"aws:PrincipalOrgID": "o-####"
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
这实际上允许具有该 OrgID 的任何人在根区域中写入记录。
在我的分区中,我就这样运行;
const subZone = new route53.PublicHostedZone(this, 'SubZone', {
zoneName: 'sub.root.zone'
});
const delegationRole = iam.Role.fromRoleArn(this, 'delegationRole', 'arn:aws:iam::###:role/###')
new route53.CrossAccountZoneDelegationRecord(this, 'delegate', {
delegatedZone: subZone,
parentHostedZoneId: '###',
delegationRole: delegationRole
});
Run Code Online (Sandbox Code Playgroud)
这最终在根区域中为我的子区域创建了委派记录。如果组织主体不适合您的用例,并且您仍然需要授予多个账户该权限,请尝试复合主体https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws -iam.CompositePrincipal.html
还想解决另一个答案中提出的关于反模式和跨账户 CDK 很难的担忧。这并不是真正的跨账户 CDK。这是利用 AWS 提供的模式(特别是启动 lambda 来执行根区域中子区域记录的配置)。
希望它对你有用!
| 归档时间: |
|
| 查看次数: |
6657 次 |
| 最近记录: |