|
14 | 14 | iam |
15 | 15 | ) |
16 | 16 |
|
17 | | -from .common import cmk_arn |
| 17 | +from .common import cmk_arn, use_aes256_encryption, use_cmk_arn |
18 | 18 | from .containers import ( |
19 | 19 | container_instance_role, |
20 | 20 | container_instance_type, |
|
69 | 69 | AllowedValues=["true", "false"], |
70 | 70 | Default="false", |
71 | 71 | ), |
72 | | - group="Global", |
| 72 | + group="Elastic Kubernetes Service (EKS)", |
73 | 73 | label="Enable EKS EncryptionConfig", |
74 | 74 | )) |
75 | 75 | use_eks_encryption_config_cond = "EnableEksEncryptionConfigCond" |
|
78 | 78 | Not(Equals(Ref(cmk_arn), "")) |
79 | 79 | )) |
80 | 80 |
|
| 81 | +# https://docs.aws.amazon.com/eks/latest/userguide/cluster-endpoint.html#modify-endpoint-access |
| 82 | +public_access_cidrs = Ref(template.add_parameter( |
| 83 | + Parameter( |
| 84 | + "EksPublicAccessCidrs", |
| 85 | + Description="The CIDR blocks that are allowed access to your cluster's public Kubernetes API server endpoint.", # noqa |
| 86 | + Type="CommaDelimitedList", |
| 87 | + Default="", |
| 88 | + ), |
| 89 | + group="Elastic Kubernetes Service (EKS)", |
| 90 | + label="Kubernetes API public access CIDRs", |
| 91 | +)) |
| 92 | +restrict_eks_api_access_cond = "RestrictEksApiAccessCond" |
| 93 | +template.add_condition(restrict_eks_api_access_cond, Not(Equals(Join("", public_access_cidrs), ""))) |
| 94 | + |
81 | 95 | # Unlike most other resources in the stack, we specify the cluster name |
82 | 96 | # via a stack parameter so it's easy to find and so it cannot be accidentally |
83 | 97 | # recreated (for example if the ResourcesVpcConfig is changed). |
|
87 | 101 | Description="The unique name to give to your cluster.", # noqa |
88 | 102 | Type="String", |
89 | 103 | ), |
90 | | - group="Global", |
| 104 | + group="Elastic Kubernetes Service (EKS)", |
91 | 105 | label="Cluster name", |
92 | 106 | )) |
93 | 107 |
|
94 | 108 | cluster = eks.Cluster( |
95 | 109 | "EksCluster", |
96 | 110 | template=template, |
97 | 111 | Name=cluster_name, |
| 112 | + Logging=eks.Logging( |
| 113 | + ClusterLogging=eks.ClusterLogging( |
| 114 | + EnabledTypes=[ |
| 115 | + eks.LoggingTypeConfig(Type="api"), |
| 116 | + eks.LoggingTypeConfig(Type="audit"), |
| 117 | + eks.LoggingTypeConfig(Type="authenticator"), |
| 118 | + ] |
| 119 | + ) |
| 120 | + ), |
98 | 121 | ResourcesVpcConfig=eks.ResourcesVpcConfig( |
99 | 122 | SubnetIds=[ |
100 | 123 | # For load balancers |
|
105 | 128 | Ref(private_subnet_b), |
106 | 129 | ], |
107 | 130 | SecurityGroupIds=[Ref(eks_security_group)], |
| 131 | + EndpointPrivateAccess=If(restrict_eks_api_access_cond, True, False), |
| 132 | + EndpointPublicAccess=True, |
| 133 | + PublicAccessCidrs=If(restrict_eks_api_access_cond, public_access_cidrs, NoValue), |
108 | 134 | ), |
109 | 135 | EncryptionConfig=If( |
110 | 136 | use_eks_encryption_config_cond, |
|
114 | 140 | RoleArn=GetAtt(eks_service_role, "Arn"), |
115 | 141 | ) |
116 | 142 |
|
| 143 | +# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-launchtemplate.html |
| 144 | +nodegroup_launch_template = ec2.LaunchTemplate( |
| 145 | + "NodegroupLaunchTemplate", |
| 146 | + template=template, |
| 147 | + LaunchTemplateData=ec2.LaunchTemplateData( |
| 148 | + BlockDeviceMappings=[ |
| 149 | + ec2.LaunchTemplateBlockDeviceMapping( |
| 150 | + DeviceName="/dev/xvda", |
| 151 | + Ebs=ec2.EBSBlockDevice( |
| 152 | + DeleteOnTermination=True, |
| 153 | + Encrypted=use_aes256_encryption, |
| 154 | + KmsKeyId=If(use_cmk_arn, Ref(cmk_arn), Ref("AWS::NoValue")), |
| 155 | + VolumeType="gp2", |
| 156 | + VolumeSize=container_volume_size, |
| 157 | + ), |
| 158 | + ), |
| 159 | + ], |
| 160 | + InstanceType=container_instance_type, |
| 161 | + MetadataOptions=ec2.MetadataOptions( |
| 162 | + HttpTokens="required", |
| 163 | + # Why 3? See note: https://github.com/adamchainz/ec2-metadata#instance-metadata-service-version-2 |
| 164 | + HttpPutResponseHopLimit=3, |
| 165 | + ), |
| 166 | + ) |
| 167 | +) |
| 168 | + |
117 | 169 | eks.Nodegroup( |
118 | 170 | # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-eks-nodegroup.html |
119 | 171 | "Nodegroup", |
|
125 | 177 | ClusterName=Ref(cluster), |
126 | 178 | # The NodeRole must be specified as an ARN. |
127 | 179 | NodeRole=GetAtt(container_instance_role, "Arn"), |
| 180 | + LaunchTemplate=eks.LaunchTemplateSpecification( |
| 181 | + Id=Ref(nodegroup_launch_template), |
| 182 | + ), |
128 | 183 | # The rest are optional. |
129 | | - DiskSize=container_volume_size, |
130 | | - InstanceTypes=[container_instance_type], |
131 | 184 | ScalingConfig=eks.ScalingConfig( |
132 | 185 | DesiredSize=desired_container_instances, |
133 | 186 | MaxSize=max_container_instances, |
|
0 commit comments