1+ # iam.tf
2+
3+ # --------------------------------------------------------------
4+ # IAM Roles & Policies for EKS
5+ # --------------------------------------------------------------
6+
7+ # Defines the IAM role that the EKS control plane will assume.
8+ resource "aws_iam_role" "eks_cluster" {
9+ name_prefix = " eks-cluster-role-"
10+
11+ assume_role_policy = jsonencode ({
12+ Version = " 2012-10-17" ,
13+ Statement = [{
14+ Action = " sts:AssumeRole" ,
15+ Effect = " Allow" ,
16+ Principal = { Service = " eks.amazonaws.com" }
17+ }]
18+ })
19+ }
20+
21+ # Attaches the required AWS-managed policy for EKS clusters to the role.
22+ resource "aws_iam_role_policy_attachment" "eks_cluster_policy" {
23+ policy_arn = " arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
24+ role = aws_iam_role. eks_cluster . name
25+ }
26+
27+
28+ # Defines the IAM role that EKS worker nodes (EC2 instances) will assume.
29+ resource "aws_iam_role" "eks_node" {
30+ name_prefix = " eks-node-role-"
31+
32+ assume_role_policy = jsonencode ({
33+ Version = " 2012-10-17" ,
34+ Statement = [{
35+ Action = " sts:AssumeRole" ,
36+ Effect = " Allow" ,
37+ Principal = { Service = " ec2.amazonaws.com" }
38+ }]
39+ })
40+ }
41+
42+ # Creates an instance profile, which is a container for the IAM role that EC2 can use.
43+ resource "aws_iam_instance_profile" "eks_node" {
44+ name_prefix = " eks-node-profile-"
45+ role = aws_iam_role. eks_node . name
46+ }
47+
48+ # Attaches the standard EKS worker node policy.
49+ resource "aws_iam_role_policy_attachment" "eks_node_worker_policy" {
50+ policy_arn = " arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
51+ role = aws_iam_role. eks_node . name
52+ }
53+
54+ # Attaches the CNI policy, allowing pods to get IP addresses from the VPC.
55+ resource "aws_iam_role_policy_attachment" "eks_node_cni_policy" {
56+ policy_arn = " arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
57+ role = aws_iam_role. eks_node . name
58+ }
59+
60+ # Attaches a read-only policy for ECR, allowing nodes to pull container images.
61+ resource "aws_iam_role_policy_attachment" "eks_node_ecr_policy" {
62+ policy_arn = " arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
63+ role = aws_iam_role. eks_node . name
64+ }
65+
66+
67+ # --------------------------------------------------------------
68+ # IAM Roles & Policies for Lambda
69+ # --------------------------------------------------------------
70+
71+ # A data source to create a reusable IAM trust policy for all Lambda functions.
72+ data "aws_iam_policy_document" "lambda_assume_role" {
73+ statement {
74+ action = " sts:AssumeRole"
75+ effect = " Allow"
76+ principals {
77+ type = " Service"
78+ identifiers = [" lambda.amazonaws.com" ]
79+ }
80+ }
81+ }
82+
83+ # Defines an execution role for the 'cortex_custom_lambda' function.
84+ resource "aws_iam_role" "cortex_custom_lambda" {
85+ name_prefix = " cortex-custom-lambda-role-"
86+ assume_role_policy = data. aws_iam_policy_document . lambda_assume_role . json
87+ }
88+
89+ # Attaches the basic execution policy, allowing the function to write to CloudWatch Logs.
90+ resource "aws_iam_role_policy_attachment" "cortex_custom_lambda_policy" {
91+ policy_arn = " arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
92+ role = aws_iam_role. cortex_custom_lambda . name
93+ }
94+
95+ # Defines an execution role for the 'empty_bucket_lambda' function.
96+ resource "aws_iam_role" "empty_bucket_lambda" {
97+ name_prefix = " empty-bucket-lambda-role-"
98+ assume_role_policy = data. aws_iam_policy_document . lambda_assume_role . json
99+ }
100+
101+ # Attaches the basic execution policy.
102+ resource "aws_iam_role_policy_attachment" "empty_bucket_lambda_policy" {
103+ policy_arn = " arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
104+ role = aws_iam_role. empty_bucket_lambda . name
105+ }
106+
107+ # Defines an execution role for the second 'cortex_custom_lambda' function.
108+ resource "aws_iam_role" "cortex_custom_lambda_2" {
109+ name_prefix = " cortex-custom-lambda-2-role-"
110+ assume_role_policy = data. aws_iam_policy_document . lambda_assume_role . json
111+ }
0 commit comments