{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "Create CodeDeploy application and deployment groups and configurations.",

  "Parameters" : {
		"Environment": {
      "Type": "String",
      "Description": "Environment Name",
			"AllowedPattern": "(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*"
    },
		"Application": {
      "Type": "String",
      "Description": "Application Name",
			"AllowedPattern": "(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*"
    },
		"EC2CodeDeployCFNStackName": {
      "Type": "String",
			"Description": "EC2 CodeDeploy Cloudformation Stack Name"
    }
  },

  "Resources" : {
		"CodeDeployApplication": {
			"Type" : "AWS::CodeDeploy::Application",
			"Properties" : {
				"ApplicationName" : { "Fn::Join" : [ "-", [ { "Ref" : "Environment" }, { "Ref" : "Application" } ] ] }
			}
		},
		"IAMAssumeCodeDeployServiceRole": {
			 "Type": "AWS::IAM::Role",
			 "Properties": {
					"AssumeRolePolicyDocument": {
						 "Version" : "2012-10-17",
						 "Statement": [ {
								"Effect": "Allow",
								"Principal": {
									 "Service": [ "codedeploy.amazonaws.com" ]
								},
								"Action": [ "sts:AssumeRole" ]
						 } ]
					},
					"ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole" ],
					"Path": "/",
					"RoleName": { "Fn::Join" : [ "-", [ "IAM", { "Ref" : "Environment" }, { "Ref" : "Application" }, "CodeDeploy", "Role" ] ] }
			 }
		},
		"CodeDeploymentGroup": {
			"Type" : "AWS::CodeDeploy::DeploymentGroup",
			"Properties" : {
				"ApplicationName" : { "Ref": "CodeDeployApplication" },
				"AutoScalingGroups" : [ { "Fn::ImportValue" : {"Fn::Sub": "${EC2CodeDeployCFNStackName}-AutoScalingGroup" } } ],
				"DeploymentConfigName" : "CodeDeployDefault.AllAtOnce",
				"DeploymentGroupName" : { "Fn::Join" : [ "-", [ { "Ref" : "Environment" }, { "Ref" : "Application" } ] ] },
				"ServiceRoleArn" : {"Fn::GetAtt" : ["IAMAssumeCodeDeployServiceRole", "Arn"] }
			}
		}
  },

  "Outputs" : {
		"StackName": {
			"Value": { "Ref": "AWS::StackName" },
      "Description": "Stack Name"
    }
  }
}

Create CodeDeploy with ASG Deployment Group
Create CodeDeploy with ASG Deployment Group
