-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsc-self-service-stack.ts
More file actions
140 lines (128 loc) · 4.74 KB
/
sc-self-service-stack.ts
File metadata and controls
140 lines (128 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { ScProductBucket } from './sc-products-stack';
import * as sc from 'aws-cdk-lib/aws-servicecatalog';
import * as cr from 'aws-cdk-lib/custom-resources';
import { PolicyStatement, Role } from 'aws-cdk-lib/aws-iam';
export class ScSelfServiceStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const params = this.node.tryGetContext('Parameters');
const tagOptions = this.node.tryGetContext('TagOptions');
// SC Portfolio
const portfolio = new sc.Portfolio(this, 'Portfolio1', {
displayName: params.PortfolioName,
description: 'Shared Products',
providerName: params.Provider,
tagOptions: new sc.TagOptions(this, 'TagOptionsPf', {
allowedValuesForTags: tagOptions.Portfolio
})
});
// Custom resource to get Organization ID
const describeOrg = new cr.AwsCustomResource(this, 'OrganizationInfo', {
onUpdate: {
service: 'Organizations',
action: 'describeOrganization',
region: 'us-east-1',
physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
},
onDelete: {
service: 'Organizations',
action: 'describeOrganization',
region: 'us-east-1',
physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
},
policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
}),
});
const orgNode = {
Type: 'ORGANIZATION',
Value: describeOrg.getResponseField('Organization.Id')
};
// Custom resource to share Portfolio with Organization
const createShare = new cr.AwsCustomResource(this, 'PortfolioShare', {
onCreate: {
service: 'ServiceCatalog',
action: 'createPortfolioShare',
region: this.region,
parameters: {
PortfolioId: portfolio.portfolioId,
OrganizationNode: orgNode,
ShareTagOptions: true
},
physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
},
onUpdate: {
service: 'ServiceCatalog',
action: 'updatePortfolioShare',
region: this.region,
parameters: {
PortfolioId: portfolio.portfolioId,
OrganizationNode: orgNode,
ShareTagOptions: true
},
physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
},
onDelete: {
service: 'ServiceCatalog',
action: 'deletePortfolioShare',
parameters: {
PortfolioId: portfolio.portfolioId,
OrganizationNode: orgNode
},
physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
},
policy: cr.AwsCustomResourcePolicy.fromStatements([
// Grant access to portfolio sharing
new PolicyStatement({
actions: [ 'servicecatalog:*PortfolioShare*' ],
resources: [ portfolio.portfolioArn ]
}),
// Grant read access to Organization's objects
new PolicyStatement({
actions: [
'organizations:ListAccount*',
'organizations:ListChildren',
'organizations:ListOrganization*',
'organizations:DescribeAccount',
'organizations:DescribeOrganization*'
],
resources: [ '*' ]
})
])
});
// Give access to list of IAM roles
for (const roleName of params.RoleAccess) {
portfolio.giveAccessToRole(Role.fromRoleName(this, `${roleName}Access`, roleName));
}
// List of SC Products
const products: sc.IProduct[] = [
new sc.CloudFormationProduct(this, 'Product1', {
productName: "Private S3 Bucket",
description: "S3 Bucket with encryption and versioning",
owner: params.Owner,
distributor: params.Distributor,
supportUrl: params.Support.Url,
supportEmail: params.Support.Email,
tagOptions: new sc.TagOptions(this, 'TagOptionsPr', {
allowedValuesForTags: tagOptions.Product
}),
productVersions: [{
productVersionName: "0.1",
cloudFormationTemplate: sc.CloudFormationTemplate.fromProductStack(
new ScProductBucket(this, 'ScProductBucket')
)
}]
})
]
// Add all Products to Portfolio and add launch constrains
for (const product of products) {
portfolio.addProduct(product);
portfolio.setLocalLaunchRoleName(product, params.Provisioning.RoleName);
}
// CF Outputs
new cdk.CfnOutput(this, 'PortfolioId', { value: portfolio.portfolioId });
new cdk.CfnOutput(this, 'OrganizationId', { value: describeOrg.getResponseField('Organization.Id') });
}
}