Search code examples
kubernetesamazon-eks

Create service Account in one liner


How can i create a service Account as a one liner using kubectl create serviceAccount test-role and then how to pass the metadata?

apiVersion: v1
kind: ServiceAccount
metadata:
  name: test-role
  namespace: utility
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::xxx:role/rolename



kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.5", GitCommit:"c285e781331a3785a7f436042c65c5641ce8a9e9", GitTreeState:"archive", BuildDate:"1980-01-01T00:00:00Z", GoVersion:"go1.17.10", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"23+", GitVersion:"v1.23.14-eks-ffeb93d", GitCommit:"96e7d52c98a32f2b296ca7f19dc9346cf79915ba", GitTreeState:"clean", BuildDate:"2022-11-29T18:43:31Z", GoVersion:"go1.17.13", Compiler:"gc", Platform:"linux/amd64"}

Solution

  • If by one line you mean one command you can use a heredoc:

    kubectl apply -f - <<EOF
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: test-role
      namespace: utility
      annotations:
        eks.amazonaws.com/role-arn: arn:aws:iam::xxx:role/rolename
    EOF
    

    Using the imperative kubectl commands, requires running two commands:

    kubectl -n utility create serviceaccount test-role
    kubectl -n utility annotate serviceaccount eks.amazonaws.com/role-arn=arn:aws:iam::xxx:role/rolename